graph algorithms
DESCRIPTION
Graph Algorithms. Introduction. Terminology V, E, directed, adjacent, path, simple path, cycle, DAG. v3. v2. v1. v5. v6. v4. v8. v7. Introduction. Terminology V, E, directed, adjacent, path, simple path, cycle, DAG. v3. v2. v1. v5. v6. v4. v8. v7. Introduction. Terminology - PowerPoint PPT PresentationTRANSCRIPT
Graph Algorithms
Introduction
• Terminology– V, E, directed, adjacent, path, simple path,
cycle, DAG
v1
v2
v4v5
v3
v7v8
v6
Introduction
• Terminology– V, E, directed, adjacent, path, simple path,
cycle, DAG
v1
v2
v4v5
v3
v7v8
v6
Introduction
• Terminology– connected, strongly connected, weakly
connected
v5
v7v8
v5
v7v8
Representation – Adjacency Matrix
v1
v2
v4v5
v3
v7v8
v6
v1 v2 v3 v4 v5 v6 v7 V8
v1
v2
v3
v4
v5
v6
V7
v8
Representation – Adjacency Matrix
v1
v2
v4v5
v3
v7v8
v6
v1 v2 v3 v4 v5 v6 v7 V8
v1 1 1 1
v2 1 1 1 1
v3 1 1
v4 1 1 1
v5 1 1 1 1
v6 1 1
v7 1 1 1 1
v8 1 1
Representation – Adjacency List
v1
v2
v4v5
v3
v7v8
v6
v2
v3
v4
v5
v6
v7
v1
v1
v2
v1
v2
v3
v1
v2
v3
v6
v2
v6
v5
v4
v4
v4
v7
v7
v5
v7
v5
v8
v8
Ø
Ø
Ø
Ø
Ø
Ø
Ø
Topological Ordering
• find vertex with no incoming edges
• print it
• remove it and its edges
v1
v2
v4v5
v3
v7v8
v6
Topological Ordering
• v1
v1
v2
v4v5
v3
v7v8
v6
v2
v4v5
v3
v7v8
v6
Topological Ordering
• v1, v2, v3, v7
v2
v4v5
v3
v7v8
v6
v4v5
v7v8
v6v5
v8
v6
v4v5
v3
v7v8
v6
v4
Topological Ordering
• v1, v2, v3, v7, v4, v5, v6, v8
• Complexity?
• Improvements?
v5
v8
v6
v8
v6v5
v8
v6v4
Topological Ordering
• Complexity – V2
• Improvements – as edges are removed, enqueue vertices with 0 indegree– v1, v2, v7, v3, v4, v5, v6, v8
v1
v2
v4v5
v3
v7v8
v6
Shortest Path Algorithms
• Given as input a weighted graph G=(V, E), and a distinguished vertex, s, find the shortest weighted path from s to every other vertex in G.
• Unweighted – every edge has weight 1
• Applications?
Breadth-first Search
• Level-order traversal
Unweighted Shortest Path
v1
v2
v4v5
v3
v7v8
v6
s.dist = 0;for(currdist = 0; currdist < NUM_VERT; currdist++) for each vertex v if(!v.known && v.dist == currdist) v.known = true for each w adjacent to v if (w.dist == INFINITY) w.dist = currdist + 1 w.path = v
Unweighted Shortest Path
v1
v2
v4v5
v3
v7v8
v6
enqueue(s)s.dist = 0;while(!q.isEmpty()) v = q.dequeue() for each w adjacent to v if(w.dist == INFINITY) w.dist = v.dist + 1 w.path = v q.enqueue(w)
Unweighted Shortest Path
queue – v1
v1
v2
v4v5
v3
v7v8
v6
dv pv
v1 0 v1
v2
v3
v4
v5
v6
v7
v8
Unweighted Shortest Path
queue – v1
queue – v2, v4, v7
queue – v4, v7, v3, v5
queue – v7, v3, v5
queue – v3, v5, v8
queue – v5, v8, v6
v1
v2
v4v5
v3
v7v8
v6
dv pv
v1 0 v1
v2 1 v1
v3 2 v2
v4 1 v1
v5 2 v2
v6 3 v3
v7 1 v1
v8 2 v7
Unweighted Shortest Path
v1
v2
v4v5
v3
v7v8
v6
enqueue(s)s.dist = 0;while(!q.isEmpty()) v = q.dequeue() for each w adjacent to v if(w.dist == INFINITY) w.dist = v.dist + 1 w.path = v q.enqueue(w)
s.dist = 0;for(currdist = 0; currdist < NUM_VERT; currdist++) for each vertex v if(!v.known && v.dist == currdist) v.known = true for each w adjacent to v if (w.dist == INFINITY) w.dist = currdist + 1 w.path = v
Dijkstra’s Algorithm
• Weighted shortest-path first• Greedy algorithm
s.dist = 0for (;;) v = smallest unknown distance vertex if(v == NOT_A_VERTEX) break; v.known = true for each w adjacent to v if(!w.known) if(v.dist + cvw < w.dist) decrease(w.dist to v.dist+cvw) w.path = v
Dijkstra’s Algorithm
v1
v2
v4v5
v3
v7v8
v6
10
7
3
2
16
2
4
5
9
2
7
known dv pv
v1 F 0 0
v2 F I 0
v3 F I 0
v4 F I 0
v5 F I 0
v6 F I 0
v7 F I 0
v8 F I 0
Dijkstra’s Algorithm
v1
v2
v4v5
v3
v7v8
v6
10
7
3
2
16
2
4
5
9
2
7
known dv pv
v1 T 0 0
v2 F 10 v1
v3 F I 0
v4 F 7 v1
v5 F I 0
v6 F I 0
v7 F 3 v1
v8 F I 0
known dv pv
v1 T 0 0
v2 F 10 v1
v3 F I 0
v4 F 5 v7
v5 F 10 v7
v6 F I 0
v7 T 3 v1
v8 F 5 v7
Dijkstra’s Algorithm
v1
v2
v4v5
v3
v7v8
v6
10
7
3
2
16
2
4
5
9
2
7
known dv pv
v1 T 0 0
v2 F 10 v1
v3 F I 0
v4 T 5 v7
v5 F 10 v7
v6 F I 0
v7 T 3 v1
v8 T 5 v7
known dv pv
v1 T 0 0
v2 F 10 v1
v3 F I 0
v4 T 5 v7
v5 F 10 v7
v6 F I 0
v7 T 3 v1
v8 F 5 v7
Dijkstra’s Algorithm
v1
v2
v4v5
v3
v7v8
v6
10
7
3
2
16
2
4
5
9
2
7
known dv pv
v1 T 0 0
v2 T 10 v1
v3 F 12 0
v4 T 5 v7
v5 F 10 v7
v6 F I 0
v7 T 3 v1
v8 T 5 v7
known dv pv
v1 T 0 0
v2 T 10 v1
v3 F 12 v2
v4 T 5 v7
v5 T 10 v7
v6 F 15 v5
v7 T 3 v1
v8 T 5 v7
Dijkstra’s Algorithm
v1
v2
v4v5
v3
v7v8
v6
10
7
3
2
16
2
4
5
9
2
7
known dv pv
v1 T 0 0
v2 T 10 v1
v3 T 12 v2
v4 T 5 v7
v5 T 10 v7
v6 T 15 v5
v7 T 3 v1
v8 T 5 v7
known dv pv
v1 T 0 0
v2 T 10 v1
v3 T 12 v2
v4 T 5 v7
v5 T 10 v7
v6 F 15 v5
v7 T 3 v1
v8 T 5 v7
Running time – Dijkstra’s
• Simple implementation– O(E + V2)
• Improvements– Use a priority queue– Smallest unknown distance vertex log V (V
times)– decrease log V (E times)
Negative Edge Costs
enqueue(s)s.dist = 0;while(!q.isEmpty()) v = q.dequeue() for each w adjacent to v if(w.dist > v.dist+cvw) w.dist = v.dist + cvw w.path = v if(w not in q) q.enqueue(w)
v1
v2
v4v5
v3
v7v8
v6
3
7
10
17
2
4
5
9
2
-6
-3
Network Flow
• Determine maximum flow from source to sink in a directed graph where each edge has given capacity – capacity cv,w is maximum flow for edge (v, w)
– total flow coming in must = total flow going out
• Example applications?
Max-Flow Algorithm
1. choose a path from src to sink – augmenting path2. add flow equal to minimum edge on path3. add reverse edges to allow algorithm to undo its
decision4. continue until no augmenting path can be found
v1
v2
v4v5
v3
v7v8
v6
10
7
3
2
16
2
4
5
9
2
7
src sink
Max-Flow Algorithm
v1
v2
v4v5
v3
v7v8
v6
10
7
3
2
16
2
4
2
9
2
4
src sink
3 3
v1
v2
v4v5
v3
v7v8
v6
87
3
2
16
2
2
2
9
2
4
src sink
3 3
22
Max-Flow Algorithm
v1
v2
v4v5
v3
v7v8
v6
67
3
2
1 4
2
2
5
9
2
4
src sink
3
42
2
v1
v2
v4v5
v3
v7v8
v6
3
2
5src sink
3
42
2
Algorithm Complexity
• O(f * E)
• Can be bad if f is large– Improve by choosing augmenting path that
increases flow by largest amount
Minimum Spanning Tree
• Find a tree (acyclic graph) that covers every vertex and has minimum cost– Number of edges in tree will be V-1
Prim’s Algorithm
• Similar to Dijkstra’s1. choose min distance vertex v and mark as
known 2. update distance values for all adjacent vertices
w1. dw = min(dw, cv, w)
v1
v2
v4v5
v3
v7v8
v6
3
7
10
17
2
4
5
9
2
6
2
Prim’s Algorithm
v1
v2
v4v5
v3
v7v8
v6
31
2
4
5
2
2
Kruskal’s Algorithm
• choose min cost edge– if it doesn’t create a cycle, add it
• use heap to provide O(ElogE) running time
v1
v2
v4v5
v3
v7v8
v6
3
7
10
17
2
4
5
9
2
6
2
Kruskal’s Algorithm
• (v2,v4), (v2,v3), (v4,v7), (v7,v8), (v1,v2), (v3,v6), (v1,v4), (v6,v5)
v1
v2
v4v5
v3
v7v8
v6
3
4
10
17
2
4
5
9
2
6
2
Depth-First Search
• Generalization of preorder traversal
dfs(Vertex v)
v.visited = true
for each w adjacent to v
if(!w.visited)
dfs(w)