270 likes | 442 Views
CS4550: Computer Networks II network layer basics 2 graphs, MSTs and SPs. network/graph algorithms. basic for finding routes through networks spanning tree algorithms - minimize the number connections needed, simplify routing Kruskal Dijkstra
E N D
CS4550:Computer Networks IInetwork layer basics 2graphs, MSTs and SPs
network/graph algorithms • basic for finding routes through networks • spanning tree algorithms - minimize the number connections needed, simplify routing • Kruskal • Dijkstra • shortest path algorithms - find “shortest” path between node pairs • Dijkstra • Bellman-Ford • Floyd-Warshall
basic graph definitions a graph G = (V,E) is a set of nodes (or points) V, and a set of edges E; each edge e = (v1,v2) is a pair of nodes from V. a path P = (v1,v2,...,vn) is a sequence of nodes, such that there is an edge between each node pair in P. graph G is connected if there is a path between every pair of nodes in G. a cycle is a path P = (v1,v2,..., vn), where v1 = vn. a subgraph g=(v,e) of G=(V,E) is a graph such that v & e are subsets of V and E, respectively.
basic graph definitions - example 7 2 3 1 8 4 6 5 G=(V,E) V= {1,2,3,4,5,6,7,8} E= {(1,2),(1,4),(1,6),(2,3),(3,4),(4,5),(5,6),(7,8)}
basic graph definitions a digraph (directed graph) is a graph in which the order of the nodes is specified; (i.e., have direction. Edges are then often called arcs). a weighted graph G=(V,E,W) is a graph/digraph in which each edge is assigned a number, or weight. a tree is a graph G with n nodes, n-1 links, and which is connected. equivalently: a tree is a graph G which is connected but has no cycles. a spanning tree T of a graph G is a subgraph which contains all of G’s nodes, but is a tree.
MST algorithms a minimal spanning tree of G is a spanning tree of least weight (# edges, weights). Kruskal : Let T represent the MST; initially T is empty. Start with least weight edge (u,v) in G, add it to T; now T ={(u,v)}. Next choose the next least weight edge, & add it to T.(If a cycle is forms, don’t use it.) Keep adding edges like this, until you have a tree T with n-1 edges, no cycles.
2 2 4 2 7 3 1 2 1 6 8 5 4 3 6 2 3 5 Kruskal MST: First T={}; then add (3,4), (1,2), (2,3), (7,8), (4,5), (6,5), (5,8).
MST : Dijkstra-Prim choose a start node v. From there, choose the least weight edge, (u,v). Now the start tree T is {(u,v)}. Next choose the least weight edge emanating from (u,v); say (u,w). Now the tree T is {(u,v),(u,w)}. Next, again choose the least weight edge from T. Repeat until T contains all the nodes of G.
Dijkstra-Prim MST 2 2 4 2 7 3 1 2 1 4 8 4 4 3 6 2 3 5 start at node 1. Then we’ll choose (1,2) first. (why?)
Dijkstra-Prim MST 2 2 7 3 1 8 4 6 5 next choose the edge connected to (1,2) which is least weight. What is it?
Dijkstra-Prim MST 2 2 2 7 3 1 8 4 6 5 next choose the edge connected to {(1,2),(2,3)} which is least weight. What is it?
Dijkstra-Prim MST 2 2 2 7 3 1 1 8 4 6 5 keep going until tree is formed. How many edges will the tree have?
Dijkstra-Prim MST 2 2 2 7 3 1 1 8 4 6 2 5 next step?
Dijkstra-Prim MST 2 2 2 7 3 1 1 8 4 6 2 3 5 next step?
Dijkstra-Prim MST 2 2 2 7 3 1 1 8 4 6 3 2 3 5 next step?
Dijkstra-Prim MST 2 2 2 7 3 2 1 1 8 4 6 3 2 3 5 final MST has a total weight of 15
Dijkstra shortest path algorithm • similar idea as the MST, but slightly more bookkeeping 1. start with a node v add it to the tree T. 2. choose least weight edge from v add it to T. 3. For each of the edges emanating from T. Calculate the total distance from start node v along each of the edges. Pick a new node with minimum distance from v. Add it to T. repeat step (3) until reaching the node desired. [key difference is in step 3]
example graph for Dijkstra SP 2 2 4 2 7 3 1 2 1 4 8 4 4 3 6 2 3 5 start at node 1. What edge should be chosen?
Dijkstra-Prim shortest path (2) 2 2 7 (0) 3 1 8 4 6 5 next choose the edge connected to (1,2) which minimizes total distance from 1. what is it? (total distance is recorded in parenthesis).
Dijkstra-Prim shortest path (2) (4) 2 2 2 7 (0) 3 1 8 4 6 5 again choose the edge which minimizes total distance from 1. what is it?
Dijkstra-Prim shortest path (2) (4) 2 2 2 7 (0) 3 1 4 8 4 (4) 6 5 total distance to node 6 is just 4, so we chose a different edge (than the MST would chose). Next?
Dijkstra-Prim shortest path (2) (4) 2 2 2 7 (0) 3 1 4 4 8 (4) 4 (4) 6 5 what is next?
Dijkstra-Prim shortest path (2) (4) 2 2 2 7 (0) 3 1 4 4 8 (4) 4 (4) 6 2 5 (6) what is next?
Dijkstra-Prim shortest path (2) 4 (4) 2 2 (8) 2 7 (0) 3 1 4 4 8 (4) 4 (4) 6 2 5 (6) what is next?
Dijkstra-Prim shortest path (2) 4 (4) 2 2 (8) 2 7 (0) 3 1 (9) 4 4 8 (4) 4 (4) 6 3 2 5 (6) Done. Computed shortest path from node 1 to all others. Distances shown in ( ). Compare to MST .
Dijkstra-Prim MST/ SP algorithms • SP algorithm will give different results when start from different node; MST algorithm always gives an MST, from any node. • to get shortest path from X to Y, start at either X or Y, and run the algorithm until you get the other. • at any step, the intermediate results in the SP show the shortest path from the source node to the others reached.
Dijkstra SP algorithm • assumption : that no edges have negative weights. O.w., won’t work. • complexity visits each node once; for each such visit, examines all the edges emanating from that node. --> orderO (n2) (n-squared, where n is the number of nodes in V) [worst case complexity] 2