Upload
xia
View
80
Download
0
Embed Size (px)
DESCRIPTION
Algorithms analysis and design. BY Lecturer: Aisha Dawood. Graph algorithms. MST Prim Kruskal Dijkstra. Minimum Spanning Tree. Problem: given a connected, undirected, weighted graph, find a spanning tree using edges that minimize the total weight. 6. 4. 5. 9. 14. 2. 10. 15. 3. - PowerPoint PPT Presentation
Citation preview
Algorithms analysis and design
BYLecturer: Aisha Dawood
Graph algorithms
● MST● Prim● Kruskal● Dijkstra
Minimum Spanning Tree
● Problem: given a connected, undirected, weighted graph, find a spanning tree using edges that minimize the total weight
1410
3
6 45
2
9
15
8
Minimum Spanning Tree
● Which edges form the minimum spanning tree (MST) of the below graph?
H B C
G E D
F
A
1410
3
6 45
2
9
15
8
Minimum Spanning Tree
● Answer:
H B C
G E D
F
A
1410
3
6 45
2
9
15
8
Minimum Spanning Tree
● MSTs satisfy the optimal substructure property: an optimal tree is composed of optimal subtrees■ Let T be an MST of G with an edge (u,v) in the middle■ Removing (u,v) partitions T into two trees T1 and T2
■ T1 is an MST of G1 = (V1,E1), and T2 is an MST of G2 = (V2,E2)
■ w(T) = w(u,v) + w(T1) + w(T2)
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
14 10
3
6 45
2
9
15
8
Run on example graph
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
14 10
3
6 45
2
9
15
8
Run on example graph
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
0
14 10
3
6 45
2
9
15
8
Pick a start vertex r
r
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
0
14 10
3
6 45
2
9
15
8
Red vertices have been removed from Q
u
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
0
3
14 10
3
6 45
2
9
15
8
Red arrows indicate parent pointers
u
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
14
0
3
14 10
3
6 45
2
9
15
8
u
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
14
0
3
14 10
3
6 45
2
9
15
8u
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
14
0 8
3
14 10
3
6 45
2
9
15
8u
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
10
0 8
3
14 10
3
6 45
2
9
15
8u
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
10
0 8
3
14 10
3
6 45
2
9
15
8u
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
10 2
0 8
3
14 10
3
6 45
2
9
15
8u
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
10 2
0 8 15
3
14 10
3
6 45
2
9
15
8u
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
10 2
0 8 15
3
14 10
3
6 45
2
9
15
8
u
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
10 2 9
0 8 15
3
14 10
3
6 45
2
9
15
8
u
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
10 2 9
0 8 15
3
4
14 10
3
6 45
2
9
15
8
u
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
5 2 9
0 8 15
3
4
14 10
3
6 45
2
9
15
8
u
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
5 2 9
0 8 15
3
4
14 10
3
6 45
2
9
15
8
u
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
5 2 9
0 8 15
3
4
14 10
3
6 45
2
9
15
8
u
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
5 2 9
0 8 15
3
4
14 10
3
6 45
2
9
15
8
u
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
5 2 9
0 8 15
3
4
14 10
3
6 45
2
9
15
8
u
Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
Total running time = O(V+V2+V.E) = O(V2), if extractmin implemented as a queue we can achieve O(V log V) if it is implemented as a heap.
Single-Source Shortest Path
● Problem: given a weighted directed graph G, find the minimum-weight path from a given source vertex s to another vertex v■ “Shortest-path” = minimum weight ■ Weight of path is sum of edges■ E.g., a road map: what is the shortest path from
Chapel Hill to Charlottesville?
Shortest Path Properties
● Again, we have optimal substructure: the shortest path consists of shortest subpaths:
Shortest Path Properties
● Define (u,v) to be the weight of the shortest path from u to v
● Shortest paths satisfy the triangle inequality: (u,v) (u,x) + (x,v)
x
u v
This path is no longer than any other path
Relaxation
● A key technique in shortest path algorithms is relaxation■ Idea: for all v, maintain upper bound d[v] on (s,v)Relax(u,v,w) { if (d[v] > d[u]+w) then d[v]=d[u]+w;}
95 2
75 2
Relax
65 2
65 2
Relax
Dijkstra’s Algorithm
● Similar to breadth-first search■ Grow a tree gradually, advancing from vertices
taken from a queue● Also similar to Prim’s algorithm for MST
■ Use a priority queue keyed on d[v]
Dijkstra’s Algorithm
Dijkstra(G) for each v V d[v] = ; d[s] = 0; S = ; Q = V; while (Q ) u = ExtractMin(Q);
enqueue(S,u); for each v u->Adj[] if (d[v] > d[u]+w(u,v)) d[v] = d[u]+w(u,v);
RelaxationStep
B
C
DA
10
4 3
2
15
Dijkstra’s Algorithm
Dijkstra(G) for each v V d[v] = ; d[s] = 0; S = ; Q = V; while (Q ) u = ExtractMin(Q);
enqueue(S,u); for each v u->Adj[] if (d[v] > d[u]+w(u,v)) d[v] = d[u]+w(u,v);
O(V)
O(V2) (since we have to search through the entire array)
O(V.E)
Total running time = O(V+V2+V.E) = O(V2), if extractmin implemented as a queue we can achieve O(V log V) if it is implemented as a heap.
Dijkstra’s Algorithm
Dijkstra’s Algorithm
Kruskal’s Algorithm
● create a set E containing all the edges in the graph
● while E is nonempty and the tree is not yet spanning■ remove an edge with minimum weight from E■ if that edge connects two different trees, then add it
to the forest, combining two trees into a single tree■ otherwise discard that edge.
Kruskal’s Algorithm
Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}
Kruskal’s Algorithm
Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}
2 199
1
5
13
1725
148
21
Run the algorithm:
Kruskal’s Algorithm
Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}
2 199
1
5
13
1725
148
21
Run the algorithm:
Kruskal’s Algorithm
Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}
2 199
1
5
13
1725
148
21
Run the algorithm:
Kruskal’s Algorithm
Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}
2 199
1?
5
13
1725
148
21
Run the algorithm:
Kruskal’s Algorithm
Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}
2 199
1
5
13
1725
148
21
Run the algorithm:
Kruskal’s Algorithm
Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}
2? 199
1
5
13
1725
148
21
Run the algorithm:
Kruskal’s Algorithm
Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}
2 199
1
5
13
1725
148
21
Run the algorithm:
Kruskal’s Algorithm
Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}
2 199
1
5?
13
1725
148
21
Run the algorithm:
Kruskal’s Algorithm
Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}
2 199
1
5
13
1725
148
21
Run the algorithm:
Kruskal’s Algorithm
Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}
2 199
1
5
13
1725
148?
21
Run the algorithm:
Kruskal’s Algorithm
Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}
2 199
1
5
13
1725
148
21
Run the algorithm:
Kruskal’s Algorithm
Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}
2 199?
1
5
13
1725
148
21
Run the algorithm:
Kruskal’s Algorithm
Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}
2 199
1
5
13
1725
148
21
Run the algorithm:
Kruskal’s Algorithm
Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}
2 199
1
5
13?
1725
148
21
Run the algorithm:
Kruskal’s Algorithm
Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}
2 199
1
5
13
1725
148
21
Run the algorithm:
Kruskal’s Algorithm
Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}
2 199
1
5
13
1725
14?8
21
Run the algorithm:
Kruskal’s Algorithm
Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}
2 199
1
5
13
1725
148
21
Run the algorithm:
Kruskal’s Algorithm
Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}
2 199
1
5
13
17?25
148
21
Run the algorithm:
Kruskal’s Algorithm
Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}
2 19?9
1
5
13
1725
148
21
Run the algorithm:
Kruskal’s Algorithm
Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}
2 199
1
5
13
1725
148
21?
Run the algorithm:
Kruskal’s Algorithm
Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}
2 199
1
5
13
1725?
148
21
Run the algorithm:
Kruskal’s Algorithm
Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}
2 199
1
5
13
1725
148
21
Run the algorithm:
Kruskal’s Algorithm
Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}
2 199
1
5
13
1725
148
21
Run the algorithm:
Kruskal’s Algorithm
Kruskal(){ T = ; for each v V MakeSet(v); sort E by increasing edge weight w for each (u,v) E (in sorted order) if FindSet(u) FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v));}
What will affect the running time? 1 Sort
O(V) MakeSet() callsO(E) FindSet() callsO(V) Union() calls
Kruskal’s Algorithm
Kruskal’s Algorithm: Running Time
● To summarize: ■ Sort edges: O(E lg E), merge sort.■ O(V) MakeSet()’s■ O(E) FindSet()’s■ O(V) Union()’s ■ Total running time O(E lg E), almost linear
without sorting
Disjoint Set Union: Analysis
● Union(A,B): “copy” elements of A into B: ??? time
● Worst-case analysis: O(n2) time for n Union’sUnion(S1, S2) “copy” 1 elementUnion(S2, S3) “copy” 2 elements…Union(Sn-1, Sn) “copy” n-1 elements
O(n2)
● Improvement: always copy smaller into larger.