1 cse 417: algorithms and computational complexity winter 2001 lecture 10 instructor: paul beame

46
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame

Post on 21-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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