Download presentation
Presentation is loading. Please wait.
Published byGwen Walker Modified over 9 years ago
1
Graphs + Shortest Paths David Kauchak cs302 Spring 2013
2
Admin
3
Tree BFS
4
Running time of Tree BFS Adjacency list How many times does it visit each vertex? How many times is each edge traversed? O(|V|+|E|) Adjacency matrix For each vertex visited, how much work is done? O(|V| 2 )
5
BFS Recursively Hard to do!
6
BFS for graphs What needs to change for graphs? Need to make sure we don’t visit a node multiple times B DE F A C G
7
distance variable keeps track of how far from the starting node and whether we’ve seen the node yet B DE F A C G
8
B DE F A C G
9
set all nodes as unseen B DE F A C G
10
check if the node has been seen B DE F A C G
11
set the node as seen and record distance B DE F A C G
12
B DE F A C G
13
B DE F A C G 0 Q: A
14
B DE F A C G 0 Q:
15
B DE F A C G 0 1 1 1 Q: D, E, B
16
B DE F A C G 0 1 1 1 Q: E, B
17
B DE F A C G 0 1 1 1 Q: B
18
B DE F A C G 0 1 1 1
19
B DE F A C G 0 1 1 1 Q:
20
B DE F A C G 0 1 1 1
21
B DE F A C G 0 1 2 2 1 1 Q: F, C
22
B DE F A C G 0 1 2 23 1 1
23
B DE F A C G 0 1 2 23 1 1
24
B DE F A C G 0 1 2 23 1 1
25
Is BFS correct? Does it visit all nodes reachable from the starting node? Can you prove it? Assume we “miss” some node ‘u’, i.e. a path exists, but we don’t visit ‘u’ S … U
26
Is BFS correct? Does it visit all nodes reachable from the starting node? Can you prove it? Find the last node along the path to ‘u’ that was visited S … U … ZW why do we know that such a node exists?
27
Is BFS correct? Does it visit all nodes reachable from the starting node? Can you prove it? We visited ‘z’ but not ‘w’, which is a contradiction, given the pseudocode S … U … ZW contradiction
28
Is BFS correct? Does it correctly label each node with the shortest distance from the starting node?
29
Is BFS correct? Does it correctly label each node with the shortest distance from the starting node? Assume the algorithm labels a node with a longer distance. Call that node ‘u’ S … U
30
Is BFS correct? Does it correctly label each node with the shortest distance from the starting node? Find the last node in the path with the correct distance S … U … ZW correct incorrect
31
Is BFS correct? Does it correctly label each node with the shortest distance from the starting node? Find the last node in the path with the correct distance S … U … ZW contradiction
32
Runtime of BFS Nothing changed over our analysis of TreeBFS
33
Runtime of BFS Adjacency list: O(|V| + |E|) Adjacency matrix: O(|V| 2 )
34
Depth First Search (DFS)
36
Tree DFS A B C E D FG
37
A B C E D FG
38
A B C E D FG
39
A B C E D FG
40
A B C E D FG Frontier?
41
Tree DFS A B C E D FG
42
A B C E D FG
43
A B C E D FG
44
A B C E D FG
45
DFS on graphs
46
mark all nodes as not visited
47
DFS on graphs until all nodes have been visited repeatedly call DFS-Visit
48
DFS on graphs What happened to the stack?
49
What does DFS do? Finds connected components Each call to DFS-Visit from DFS starts exploring a new set of connected components Helps us understand the structure/connectedness of a graph
50
Is DFS correct? Does DFS visit all of the nodes in a graph?
51
Running time? Like BFS Visits each node exactly once Processes each edge exactly twice (for an undirected graph) O(|V|+|E|)
52
DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch
53
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
54
Topological sort
55
underwear pants belt shirt tie jacket socks shoes watch
56
Topological sort underwear pants belt shirt tie jacket socks shoes watch
57
Topological sort underwear pants belt shirt tie jacket socks shoes watch
58
Topological sort underwear pants belt shirt tie jacket socks shoes watch
59
Topological sort underwear pants belt shirt tie jacket socks shoes watch
60
Topological sort underwear pants belt shirt tie jacket socks shoes watch
61
Topological sort underwear pants belt shirt tie jacket socks shoes watch
62
Topological sort underwear pants belt shirt tie jacket socks shoes watch …
63
Running time?
64
O(|V|+|E|)
65
Running time? O(E) overall
66
Running time? How many calls? |V|
67
Running time? Overall running time? O(|V| 2 +|V| |E|)
68
Can we do better?
69
Topological sort 2
73
Running time? How many times do we process each node? How many times do we process each edge? O(|V| + |E|)
74
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|)
75
Strongly connected Given a directed graph, can we reach any node v from any other node u? Ideas?
76
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|)
77
Strongly connected
78
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 ……
79
Runtime? O(|V| + |E|) O(|V|)
80
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
81
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
82
Shortest paths What is the shortest path from a to d? A B CE D
83
Shortest paths BFS A B CE D
84
Shortest paths What is the shortest path from a to d? A B CE D 1 1 3 2 2 3 4
85
Shortest paths We can still use BFS A B CE D 1 1 3 2 2 3 4
86
Shortest paths We can still use BFS A B CE D 1 1 3 2 2 3 4 A B CE D
87
Shortest paths We can still use BFS A B CE D
88
Shortest paths What is the problem? A B CE D
89
Shortest paths Running time is dependent on the weights A B C 4 1 2 A B C 200 50 100
90
Shortest paths A B C 200 50 100 A B C
91
Shortest paths A B C
92
A B C
93
A B C Nothing will change as we expand the frontier until we’ve gone out 100 levels
94
Dijkstra’s algorithm
96
prev keeps track of the shortest path
97
Dijkstra’s algorithm
100
A B CE D 1 1 3 3 2 1 4
101
A B CE D 1 1 3 3 2 1 4
102
A B CE D 1 1 3 3 2 1 4 0 Heap A 0 B C D E
103
A B CE D 1 1 3 3 2 1 4 0 Heap B C D E
104
A B CE D 1 1 3 3 2 1 4 0 Heap B C D E
105
A B CE D 1 1 3 3 2 1 4 1 0 Heap C 1 B D E
106
A B CE D 1 1 3 3 2 1 4 1 0 Heap C 1 B D E
107
A B CE D 1 1 3 3 2 1 4 3 1 0 Heap C 1 B 3 D E
108
A B CE D 1 1 3 3 2 1 4 3 1 0 Heap C 1 B 3 D E
109
3 A B CE D 1 1 3 2 1 4 3 1 0 Heap B 3 D E
110
3 A B CE D 1 1 3 2 1 4 3 1 0 Heap B 3 D E
111
3 A B CE D 1 1 3 2 1 4 3 1 0 Heap B 3 D E
112
3 A B CE D 1 1 3 2 1 4 2 1 0 Heap B 2 D E
113
3 A B CE D 1 1 3 2 1 4 2 1 0 Heap B 2 D E
114
3 A B CE D 1 1 3 2 1 4 2 5 1 0 Heap B 2 E 5 D
115
3 A B CE D 1 1 3 2 1 4 2 5 1 0 Heap B 2 E 5 D Frontier?
116
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
117
3 A B CE D 1 1 3 2 1 4 25 3 1 0 Heap E 3 D 5
118
3 A B CE D 1 1 3 2 1 4 25 3 1 0 Heap D 5
119
3 A B CE D 1 1 3 2 1 4 25 3 1 0 Heap
120
A B CE D 1 1 1 25 3 1 0 3
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.