Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch.

Similar presentations


Presentation on theme: "Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch."— Presentation transcript:

1 Graphs David Kauchak cs302 Spring 2012

2 DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

3 Topological sort A linear ordering of all the vertices such that for all edges (u,v)  E, u appears before v in the ordering An ordering of the nodes that “obeys” the dependencies, i.e. an activity can’t happen until it’s dependent activities have happened underwear pants belt shirt tie jacket socks shoes watch underwear pants belt watch shirt tie socks shoes jacket

4 Topological sort

5 underwear pants belt shirt tie jacket socks shoes watch

6 Topological sort underwear pants belt shirt tie jacket socks shoes watch

7 Topological sort underwear pants belt shirt tie jacket socks shoes watch

8 Topological sort underwear pants belt shirt tie jacket socks shoes watch

9 Topological sort underwear pants belt shirt tie jacket socks shoes watch

10 Topological sort underwear pants belt shirt tie jacket socks shoes watch

11 Topological sort underwear pants belt shirt tie jacket socks shoes watch

12 Topological sort underwear pants belt shirt tie jacket socks shoes watch …

13 Running time?

14 O(|V|+|E|)

15 Running time? O(E) overall

16 Running time? How many calls? |V|

17 Running time? Overall running time? O(|V| 2 +|V| |E|)

18 Can we do better?

19 Topological sort 2

20

21

22

23 Running time? How many times do we process each node? How many times do we process each edge? O(|V| + |E|)

24 Connectedness Given an undirected graph, for every node u  V, can we reach all other nodes in the graph? Run BFS or DFS-Visit (one pass) and mark nodes as we visit them. If we visit all nodes, return true, otherwise false. Running time:O(|V| + |E|)

25 Strongly connected Given a directed graph, can we reach any node v from any other node u? Ideas?

26 Transpose of a graph Given a graph G, we can calculate the transpose of a graph G R by reversing the direction of all the edges A B C E D A B C E D G GRGR Running time to calculate G R ?O(|V| + |E|)

27 Strongly connected

28 Is it correct? What do we know after the first pass? Starting at u, we can reach every node What do we know after the second pass? All nodes can reach u. Why? We can get from u to every node in G R, therefore, if we reverse the edges (i.e. G), then we have a path from every node to u Which means that any node can reach any other node. Given any two nodes s and t we can create a path through u sut ……

29 Runtime? O(|V| + |E|) O(|V|)

30 Detecting cycles Undirected graph BFS or DFS. If we reach a node we’ve seen already, then we’ve found a cycle Directed graph A B D have to be careful

31 Detecting cycles Undirected graph BFS or DFS. If we reach a node we’ve seen already, then we’ve found a cycle Directed graph Call TopologicalSort If the length of the list returned ≠ |V| then a cycle exists

32 Shortest paths What is the shortest path from a to d? A B CE D

33 Shortest paths BFS A B CE D

34 Shortest paths What is the shortest path from a to d? A B CE D 1 1 3 2 2 3 4

35 Shortest paths We can still use BFS A B CE D 1 1 3 2 2 3 4

36 Shortest paths We can still use BFS A B CE D 1 1 3 2 2 3 4 A B CE D

37 Shortest paths We can still use BFS A B CE D

38 Shortest paths What is the problem? A B CE D

39 Shortest paths Running time is dependent on the weights A B C 4 1 2 A B C 200 50 100

40 Shortest paths A B C 200 50 100 A B C

41 Shortest paths A B C

42 A B C

43 A B C Nothing will change as we expand the frontier until we’ve gone out 100 levels

44 Dijkstra’s algorithm

45

46 prev keeps track of the shortest path

47 Dijkstra’s algorithm

48

49

50 Single source shortest paths All of the shortest path algorithms we’ll look at today are call “single source shortest paths” algorithms Why?

51 A B CE D 1 1 3 3 2 1 4

52 A B CE D 1 1 3 3 2 1 4    

53 A B CE D 1 1 3 3 2 1 4    0 Heap A 0 B  C  D  E 

54 A B CE D 1 1 3 3 2 1 4    0 Heap B  C  D  E 

55 A B CE D 1 1 3 3 2 1 4    0 Heap B  C  D  E 

56 A B CE D 1 1 3 3 2 1 4   1 0 Heap C 1 B  D  E 

57 A B CE D 1 1 3 3 2 1 4   1 0 Heap C 1 B  D  E 

58 A B CE D 1 1 3 3 2 1 4 3   1 0 Heap C 1 B 3 D  E 

59 A B CE D 1 1 3 3 2 1 4 3   1 0 Heap C 1 B 3 D  E 

60 3 A B CE D 1 1 3 2 1 4 3   1 0 Heap B 3 D  E 

61 3 A B CE D 1 1 3 2 1 4 3   1 0 Heap B 3 D  E 

62 3 A B CE D 1 1 3 2 1 4 3   1 0 Heap B 3 D  E 

63 3 A B CE D 1 1 3 2 1 4 2   1 0 Heap B 2 D  E 

64 3 A B CE D 1 1 3 2 1 4 2   1 0 Heap B 2 D  E 

65 3 A B CE D 1 1 3 2 1 4 2  5 1 0 Heap B 2 E 5 D 

66 3 A B CE D 1 1 3 2 1 4 2  5 1 0 Heap B 2 E 5 D  Frontier?

67 3 A B CE D 1 1 3 2 1 4 2  5 1 0 Heap B 2 E 5 D  All nodes reachable from starting node within a given distance

68 3 A B CE D 1 1 3 2 1 4 25 3 1 0 Heap E 3 D 5

69 3 A B CE D 1 1 3 2 1 4 25 3 1 0 Heap D 5

70 3 A B CE D 1 1 3 2 1 4 25 3 1 0 Heap

71 A B CE D 1 1 1 25 3 1 0 3

72 Is Dijkstra’s algorithm correct? Invariant:

73 Is Dijkstra’s algorithm correct? Invariant: For every vertex removed from the heap, dist[v] is the actual shortest distance from s to v proof?

74 Is Dijkstra’s algorithm correct? Invariant: For every vertex removed from the heap, dist[v] is the actual shortest distance from s to v The only time a vertex gets visited is when the distance from s to that vertex is smaller than the distance to any remaining vertex Therefore, there cannot be any other path that hasn’t been visited already that would result in a shorter path

75 Running time?

76 1 call to MakeHeap

77 Running time? |V| iterations

78 Running time? |V| calls

79 Running time? O(|E|) calls

80 Running time? Depends on the heap implementation 1 MakeHeap|V| ExtractMin|E| DecreaseKeyTotal ArrayO(|V|)O(|V| 2 ) O(|E|) O(|V| 2 ) Bin heapO(|V|)O(|V| log |V|) O(|E| log |V|) O((|V|+|E|) log |V|) O(|E| log |V|)

81 Running time? Depends on the heap implementation 1 MakeHeap|V| ExtractMin|E| DecreaseKeyTotal ArrayO(|V|)O(|V| 2 ) O(|E|) O(|V| 2 ) Bin heapO(|V|)O(|V| log |V|) O(|E| log |V|) O((|V|+|E|) log |V|) O(|E| log |V|) Is this an improvement?If |E| < |V| 2 / log |V|

82 Running time? Depends on the heap implementation 1 MakeHeap|V| ExtractMin|E| DecreaseKeyTotal ArrayO(|V|)O(|V| 2 ) O(|E|) O(|V| 2 ) Bin heapO(|V|)O(|V| log |V|) O(|E| log |V|) O((|V|+|E|) log |V|) Fib heapO(|V|)O(|V| log |V|) O(|E|) O(|V| log |V| + |E|) O(|E| log |V|)

83 What about Dijkstra’s on…? A B C E D 1 1 -10 5 10

84 What about Dijkstra’s on…? A B C E D 1 1 -10 5 10

85 What about Dijkstra’s on…? A B C E D 1 1 5 10 Dijkstra’s algorithm only works for positive edge weights


Download ppt "Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch."

Similar presentations


Ads by Google