1 cse 417: algorithms and computational complexity winter 2001 lecture 10 instructor: paul beame
Post on 21-Dec-2015
216 views
TRANSCRIPT
![Page 1: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/1.jpg)
1
CSE 417: Algorithms and Computational Complexity
Winter 2001
Lecture 10
Instructor: Paul Beame
![Page 2: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/2.jpg)
2
DFS(v) for a directed graph
1
210
9
8
3
4
5
6
7
1112
13
![Page 3: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/3.jpg)
3
DFS(v)1
210
9
8
3
4
5
6
7
1112
13
tree edges
back edges
forward edges
cross edges
NO cross edges
![Page 4: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/4.jpg)
4
Strongly-connected components
In directed graph if there is a path from a to b there might not be one from b to a
a and b are strongly connected iff there is a path in both directions (i.e. a directed cycle containing both a and b
Breaks graph into components
![Page 5: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/5.jpg)
5
Strongly-connected components
1
210
9
8
3
4
5
6
7
1112
13
![Page 6: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/6.jpg)
6
Uses for SCC’s
Optimizing compilers need to find loops, which are SCC’s in the program flow graph.
If (u,v) means process u is waiting for process v, SCC’s show deadlocks.
![Page 7: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/7.jpg)
7
Directed Acyclic Graphs
If we collapse each SCC to a single vertex we get a directed graph with no cycles a directed acyclic graph or DAG
Many problems on directed graphs can be solved as follows: Compute SCC’s and resulting DAG Do one computation on each SCC Do another computation on the overall DAG
![Page 8: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/8.jpg)
8
Simple SCC Algorithm
u,v in same SCC iff there are paths u v & v u
DFS from every u, v: O(nm) = O(n3)
![Page 9: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/9.jpg)
9
Better method
Can compute all the SCC’s while doing a single DFS! O(n+m) time
We won’t do the full algorithm but will give some ideas
![Page 10: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/10.jpg)
10
Definition
The root of an SCC is the first vertex in it visited by DFS.
Equivalently, the root is the vertex in the SCC with the smallest number in DFS ordering.
![Page 11: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/11.jpg)
11
Subgoal
All members of an SCC are descendants of its root.
Can we identify some root?
How about the root of the first SCC completely explored by DFS?
Key idea: no exit from first SCC(first SCC is leftmost “leaf” in collapsed DAG)
![Page 12: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/12.jpg)
12
Definition
x is an exit from v (from v’s subtree) if x is not a descendant of v, but x is the head of a (cross- or back-) edge from
a descendant of v (including v itself)
Any non-root vertex v has an exit
vx
vx
![Page 13: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/13.jpg)
13
Finding SCC’s
A root nodes v sometimes have exits only via a cross-edge to a node x that is not in
a component with a root above v, e.g. vertex 10 in the example.
![Page 14: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/14.jpg)
14
Strongly-connected components
1
210
9
8
3
4
5
6
7
1112
13
![Page 15: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/15.jpg)
15
Minimum Spanning Trees (Forests)
Given an undirected graph G=(V,E) with each edge e having a weight w(e)
Find a subgraph T of G of minimum total weight s.t. every pair of vertices connected in G are also connected in T if G is connected then T is a tree otherwise it
is a forest
![Page 16: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/16.jpg)
16
Weighted Undirected Graph
2
7
-1
43
4
5
13
58
6
94
579
8
1
23
10
5
4
9
12
8
13
67
11
![Page 17: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/17.jpg)
17
First Greedy Algorithm
Prim’s Algorithm: start at a vertex v add the cheapest edge adjacent to v repeatedly add the cheapest edge that joins
the vertices explored so far to the rest of the graph.
We’ll show it works later
![Page 18: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/18.jpg)
18
Prim’s Algorithm
2
7
-1
43
4
5
13
58
6
94
579
8
1
23
10
5
4
9
12
8
13
67
11
![Page 19: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/19.jpg)
19
Prim’s Algorithm
2
7
-1
43
4
5
13
58
6
94
579
8
1
23
10
5
4
9
12
8
13
67
11
![Page 20: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/20.jpg)
20
Prim’s Algorithm
2
7
-1
43
4
5
13
58
6
94
579
8
1
23
10
5
4
9
12
8
13
67
11
![Page 21: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/21.jpg)
21
Prim’s Algorithm
2
7
-1
43
4
5
13
58
6
94
579
8
1
23
10
5
4
9
12
8
13
67
11
![Page 22: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/22.jpg)
22
Prim’s Algorithm
2
7
-1
43
4
5
13
58
6
94
579
8
1
23
10
5
4
9
12
8
13
67
11
![Page 23: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/23.jpg)
23
Prim’s Algorithm
2
7
-1
43
4
5
13
58
6
94
579
8
1
23
10
5
4
9
12
8
13
67
11
![Page 24: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/24.jpg)
24
Prim’s Algorithm
2
7
-1
43
4
5
13
58
6
94
579
8
1
23
10
5
4
9
12
8
13
67
11
![Page 25: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/25.jpg)
25
Prim’s Algorithm
2
7
-1
43
4
5
13
58
6
94
579
8
1
23
10
5
4
9
12
8
13
67
11
![Page 26: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/26.jpg)
26
Prim’s Algorithm
2
7
-1
43
4
5
13
58
6
94
579
8
1
23
10
5
4
9
12
8
13
67
11
![Page 27: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/27.jpg)
27
Prim’s Algorithm
2
7
-1
43
4
5
13
58
6
94
579
8
1
23
10
5
4
9
12
8
13
67
11
![Page 28: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/28.jpg)
28
Prim’s Algorithm
2
7
-1
43
4
5
13
58
6
94
579
8
1
23
10
5
4
9
12
8
13
67
11
![Page 29: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/29.jpg)
29
Prim’s Algorithm
2
7
-1
43
4
5
13
58
6
94
579
8
1
23
10
5
4
9
12
8
13
67
11
![Page 30: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/30.jpg)
30
Prim’s Algorithm
2
7
-1
43
4
5
13
58
6
94
579
8
1
23
10
5
4
9
12
8
13
67
11
MST weight =41
![Page 31: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/31.jpg)
31
Second Greedy Algorithm
Kruskal’s Algorithm Start with the vertices and no edges Repeatedly add the cheapest edge that joins
two different components. i.e. that doesn’t create a cycle
Again we save the proof of correctness for later
![Page 32: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/32.jpg)
32
Kruskal’s Algorithm
2
7
-1
43
4
5
13
58
6
94
579
8
1
23
10
5
4
9
12
8
13
67
11
![Page 33: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/33.jpg)
33
Kruskal’s Algorithm
2
7
-1
43
4
5
13
58
6
94
579
8
1
23
10
5
4
9
12
8
13
67
11
![Page 34: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/34.jpg)
34
Kruskal’s Algorithm
2
7
-1
43
4
5
13
58
6
94
579
8
1
23
10
5
4
9
12
8
13
67
11
![Page 35: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/35.jpg)
35
Kruskal’s Algorithm
2
7
-1
43
4
5
13
58
6
94
579
8
1
23
10
5
4
9
12
8
13
67
11
![Page 36: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/36.jpg)
36
Kruskal’s Algorithm
2
7
-1
43
4
5
13
58
6
94
579
8
1
23
10
5
4
9
12
8
13
67
11
![Page 37: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/37.jpg)
37
Kruskal’s Algorithm
2
7
-1
43
4
5
13
58
6
94
579
8
1
23
10
5
4
9
12
8
13
67
11
![Page 38: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/38.jpg)
38
Kruskal’s Algorithm
2
7
-1
43
4
5
13
58
6
94
579
8
1
23
10
5
4
9
12
8
13
67
11
![Page 39: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/39.jpg)
39
Kruskal’s Algorithm
2
7
-1
43
4
5
13
58
6
94
579
8
1
23
10
5
4
9
12
8
13
67
11
![Page 40: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/40.jpg)
40
Kruskal’s Algorithm
2
7
-1
43
4
5
13
58
6
94
579
8
1
23
10
5
4
9
12
8
13
67
11
![Page 41: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/41.jpg)
41
Kruskal’s Algorithm
2
7
-1
43
4
5
13
58
6
94
579
8
1
23
10
5
4
9
12
8
13
67
11
![Page 42: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/42.jpg)
42
Kruskal’s Algorithm
2
7
-1
43
4
5
13
58
6
94
579
8
1
23
10
5
4
9
12
8
13
67
11
![Page 43: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/43.jpg)
43
Kruskal’s Algorithm
2
7
-1
43
4
5
13
58
6
94
579
8
1
23
10
5
4
9
12
8
13
67
11
![Page 44: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/44.jpg)
44
Kruskal’s Algorithm
2
7
-1
43
4
5
13
58
6
94
579
8
1
23
10
5
4
9
12
8
13
67
11
![Page 45: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/45.jpg)
45
Kruskal’s Algorithm
2
7
-1
43
4
5
13
58
6
94
579
8
1
23
10
5
4
9
12
8
13
67
11
![Page 46: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame](https://reader031.vdocuments.net/reader031/viewer/2022032310/56649d625503460f94a453ed/html5/thumbnails/46.jpg)
46
Kruskal’s Algorithm
2
7
-1
43
4
5
13
58
6
94
579
8
1
23
10
5
4
9
12
8
13
67
11
produces same tree as Prim’s algorithm