cs4550: computer networks ii network layer basics 2 graphs, msts and sps

27
CS4550: CS4550: Computer Networks II Computer Networks II network layer basics 2 network layer basics 2 graphs, MSTs and SPs graphs, MSTs and SPs

Upload: kelly-moody

Post on 03-Jan-2016

30 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS4550: Computer Networks  II network layer basics 2 graphs, MSTs and SPs

CS4550:CS4550:

Computer Networks IIComputer Networks II

network layer basics 2network layer basics 2

graphs, MSTs and SPs graphs, MSTs and SPs

Page 2: CS4550: Computer Networks  II network layer basics 2 graphs, MSTs and SPs

network/graph algorithms 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

Page 3: CS4550: Computer Networks  II network layer basics 2 graphs, MSTs and SPs

basic graph definitions 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.

Page 4: CS4550: Computer Networks  II network layer basics 2 graphs, MSTs and SPs

basic graph definitions - basic graph definitions - example example

2

5

64

31

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)}

7

8

Page 5: CS4550: Computer Networks  II network layer basics 2 graphs, MSTs and SPs

basic graph definitionsbasic 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.

Page 6: CS4550: Computer Networks  II network layer basics 2 graphs, MSTs and SPs

MST algorithms 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.

Page 7: CS4550: Computer Networks  II network layer basics 2 graphs, MSTs and SPs

2

5

64

317

8

3

22

56 1

23

2

4

First T={}; then add (3,4), (1,2), (2,3), (7,8), (4,5), (6,5), (5,8).

Kruskal MST:

Page 8: CS4550: Computer Networks  II network layer basics 2 graphs, MSTs and SPs

MST : Dijkstra-Prim 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.

Page 9: CS4550: Computer Networks  II network layer basics 2 graphs, MSTs and SPs

Dijkstra-Prim MSTDijkstra-Prim MST

2

5

64

317

8

3

22

44 1

23

2

4

start at node 1. Then we’ll choose (1,2) first. (why?)

Page 10: CS4550: Computer Networks  II network layer basics 2 graphs, MSTs and SPs

Dijkstra-Prim MST Dijkstra-Prim MST

2

5

64

317

8

2

next choose the edge connected to (1,2) which is least weight. What is it?

Page 11: CS4550: Computer Networks  II network layer basics 2 graphs, MSTs and SPs

Dijkstra-Prim MST Dijkstra-Prim MST

2

5

64

317

8

2

next choose the edge connected to {(1,2),(2,3)} which is least weight. What is it?

2

Page 12: CS4550: Computer Networks  II network layer basics 2 graphs, MSTs and SPs

Dijkstra-Prim MST Dijkstra-Prim MST

2

5

64

317

8

2

keep going until tree is formed. How many edges will the tree have?

2

1

Page 13: CS4550: Computer Networks  II network layer basics 2 graphs, MSTs and SPs

Dijkstra-Prim MST Dijkstra-Prim MST

2

5

64

317

8

2 2

1

2

next step?

Page 14: CS4550: Computer Networks  II network layer basics 2 graphs, MSTs and SPs

Dijkstra-Prim MST Dijkstra-Prim MST

2

5

64

317

8

2 2

1

2

next step?

3

Page 15: CS4550: Computer Networks  II network layer basics 2 graphs, MSTs and SPs

Dijkstra-Prim MST Dijkstra-Prim MST

2

5

64

317

8

2 2

1

2

next step?

33

Page 16: CS4550: Computer Networks  II network layer basics 2 graphs, MSTs and SPs

Dijkstra-Prim MST Dijkstra-Prim MST

2

5

64

317

8

2 2

1

2

final MST has a total weight of 15

33

2

Page 17: CS4550: Computer Networks  II network layer basics 2 graphs, MSTs and SPs

Dijkstra shortest path algorithm 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]

Page 18: CS4550: Computer Networks  II network layer basics 2 graphs, MSTs and SPs

example graph for Dijkstra SP example graph for Dijkstra SP

2

5

64

317

8

3

22

44 1

23

2

4

start at node 1. What edge should be chosen?

Page 19: CS4550: Computer Networks  II network layer basics 2 graphs, MSTs and SPs

Dijkstra-Prim shortest path Dijkstra-Prim shortest path

2

5

64

317

8

2

next choose the edge connected to (1,2) which minimizes total distance from 1. what is it? (total distance is recorded in parenthesis).

(0)

(2)

Page 20: CS4550: Computer Networks  II network layer basics 2 graphs, MSTs and SPs

Dijkstra-Prim shortest path Dijkstra-Prim shortest path

2

5

64

317

8

2

again choose the edge which minimizes total distance from 1. what is it?

(0)

(2)2 (4)

Page 21: CS4550: Computer Networks  II network layer basics 2 graphs, MSTs and SPs

Dijkstra-Prim shortest path Dijkstra-Prim shortest path

2

5

64

317

8

2

total distance to node 6 is just 4, so we chose a different edge (than the MST would chose). Next?

(0)

(2)2 (4)

4

(4)

Page 22: CS4550: Computer Networks  II network layer basics 2 graphs, MSTs and SPs

Dijkstra-Prim shortest path Dijkstra-Prim shortest path

2

5

64

317

8

2

what is next?

(0)

(2)2 (4)

4

(4)

4 (4)

Page 23: CS4550: Computer Networks  II network layer basics 2 graphs, MSTs and SPs

Dijkstra-Prim shortest path Dijkstra-Prim shortest path

2

5

64

317

8

2

what is next?

(0)

(2)2 (4)

4

(4)

4 (4)

2

(6)

Page 24: CS4550: Computer Networks  II network layer basics 2 graphs, MSTs and SPs

Dijkstra-Prim shortest path Dijkstra-Prim shortest path

2

5

64

317

8

2

what is next?

(0)

(2)2 (4)

4

(4)

4 (4)

2

(6)

4 (8)

Page 25: CS4550: Computer Networks  II network layer basics 2 graphs, MSTs and SPs

Dijkstra-Prim shortest path Dijkstra-Prim shortest path

2

5

64

317

8

2

Done. Computed shortest path from node 1 to all others. Distances shown in ( ). Compare to MST .

(0)

(2)2 (4)

4

(4)

4 (4)

2

(6)

4 (8)

3

(9)

Page 26: CS4550: Computer Networks  II network layer basics 2 graphs, MSTs and SPs

Dijkstra-Prim MST/ SP algorithmsDijkstra-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.

Page 27: CS4550: Computer Networks  II network layer basics 2 graphs, MSTs and SPs

Dijkstra SP algorithm 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.

--> order O (n2 ) (n-squared, where n is the number of nodes in V) [worst case complexity]

2