cse 373: data structures and algorithms lecture 18: graphs ii 1

55
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

Post on 21-Dec-2015

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

CSE 373: Data Structures and Algorithms

Lecture 18: Graphs II

1

Page 2: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

Directed graphs• directed graph (digraph): edges are one-way

connections between vertices

2

Page 3: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

3

Trees as Graphs

• Every tree is a graph with some restrictions:

–the tree is directed–the tree is acyclic–there is exactly one

directed path from the root to every node

A

B

D E

C

F

HG

Page 4: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

More terminology

• degree: number of edges touching a vertex– example: W has degree 4– what is the degree of X? of Z?

• adjacent vertices: connecteddirectly by an edge

• If graph is directed, a vertex has a separate in/out degree

XU

V

W

Z

Y

a

c

b

ed

f

g

h

i

j

4

Page 5: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

Graph questions• Are the following graphs directed or not

directed?– Buddy graphs of instant messaging programs?

(vertices = users, edges = user being on another's buddy list)– bus line graph depicting all of Seattle's bus stations and

routes– graph of movies in which actors have appeared together

• Are these graphs potentially cyclic? Why or why not?

5

Page 6: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

Graph exercise• Consider a graph of instant messenger buddies.

– What do the vertices represent? What does an edge represent?– Is this graph directed or undirected? Weighted or unweighted?– What does a vertex's degree mean? In degree? Out degree?– Can the graph contain loops? cycles?

• Consider this graph data:– Jessica's buddy list: Meghan, Alan, Martin.– Meghan's buddy list: Alan, Lori.– Toni's buddy list: Lori, Meghan.– Martin's buddy list: Lori, Meghan.– Alan's buddy list: Martin, Jessica.– Lori's buddy list: Meghan.

– Compute the in/out degree of each vertex. Is the graph connected?– Who is the most popular? Least? Who is the most antisocial?– If we're having a party and want to distribute the message the most quickly, who should we

tell first?

6

Page 7: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

7

Topological Sort

321143322

326

341370

378

401

421

Problem: Find an order inwhich all these courses can be taken.

Example: 142 143 378 370 321 341 322 326 421 401

In order to take a course, you must take all of its prerequisites first

142

Page 8: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

8

Given a digraph G = (V, E), find a total ordering of its vertices such that:

for any edge (v, w) in E, v precedes w in the ordering

A

BC

F

D E

Topological Sort

Page 9: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

9

A

BC

F

D E

EA DFB C

Any total ordering in whichall the arrows go to the rightis a valid solution

Topo sort - good example

Note that F can go anywhere in this list because it is not connected.Also the solution is not unique.

Page 10: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

10

A

BC

F

D E

DA EFB C

Any ordering in whichan arrow goes to the leftis not a valid solution

Topo sort - bad example

NO!

Page 11: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

11

Only acyclic graphs can be topologically sorted

• A directed graph with a cycle cannot be topologically sorted.

A

BC

F

D E

Page 12: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

12

Step 1: Identify vertices that have no incoming edges• The “in-degree” of these vertices is zero

A

BC

F

D E

Topological sort algorithm: 1

Page 13: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

13

Step 1: Identify vertices that have no incoming edges• If no such vertices, graph has only cycle(s) (cyclic graph)• Topological sort not possible – Halt.

A

BC

DExample of a cyclic graph

Topo sort algorithm: 1a

Page 14: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

14

Step 1: Identify vertices that have no incoming edges• Select one such vertex

A

BC

F

D E

Select

Topo sort algorithm:1b

Page 15: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

15

A

BC

F

D E

Step 2: Delete this vertex of in-degree 0 and all its outgoing edges from the graph. Place it in the output.

Topo sort algorithm: 2

A

Page 16: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

16

Repeat Step 1 and Step 2 until graph is empty

A

BC

F

D E

Select

Continue until done

Page 17: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

17

A

BC

F

D E

B

Select B. Copy to sorted list. Delete B and its edges.

B

Page 18: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

18

A

C

F

D E

B C

Select C. Copy to sorted list. Delete C and its edges.

C

Page 19: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

19

AF

D E

B C D

Select D. Copy to sorted list. Delete D and its edges.

D

Page 20: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

20

AF

E

B C D E F

Select E. Copy to sorted list. Delete E and its edges.Select F. Copy to sorted list. Delete F and its edges.

E, F

Page 21: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

21

A B C D E F

Done

A

BC

F

D E

Page 22: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

22

Topological Sort Algorithm

1. Store each vertex’s In-Degree in an array D2. Initialize queue with all “in-degree=0” vertices3. While there are vertices remaining in the queue:

(a) Dequeue and output a vertex(b) Reduce In-Degree of all vertices adjacent to it by 1(c) Enqueue any of these vertices whose In-Degree became

zero4. If all vertices are output then success, otherwise

there is a cycle.

Page 23: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

23

Pseudocode

Queue Q := [Vertices with in-degree 0]while notEmpty(Q) do x := Dequeue(Q) Output(x) y := A[x]; // y gets a linked list of vertices while y null do D[y.value] := D[y.value] – 1; if D[y.value] = 0 then Enqueue(Q,y.value); y := y.next; endwhileendwhile

Page 24: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

24

1

2 36

4 5

0

1

2

10

2

Queue (before): Queue (after): 1, 6

Answer:

Topo Sort w/ queue

Page 25: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

25

12 3

6

4 5

0

0

1

10

2

Queue (before): 1, 6Queue (after): 6, 2

Answer: 1

Topo Sort w/ queue

Page 26: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

26

12 3

6

4 5

0

0

1

10

2

Queue (before): 6, 2Queue (after): 2

Answer: 1, 6

Topo Sort w/ queue

Page 27: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

27

12 3

6

4 5

0

0

1

00

2

Queue (before): 2Queue (after): 3

Answer: 1, 6, 2

Topo Sort w/ queue

Page 28: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

28

12 3

6

4 5

0

0

0

00

1

Queue (before): 3Queue (after): 4

Answer: 1, 6, 2, 3

Topo Sort w/ queue

Page 29: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

29

12 3

6

4 5

0

0

0

00

0

Queue (before): 4Queue (after): 5

Answer: 1, 6, 2, 3, 4

Topo Sort w/ queue

Page 30: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

30

12 3

6

4 5

0

0

0

00

0

Queue (before): 5Queue (after):

Answer: 1, 6, 2, 3, 4, 5

Topo Sort w/ queue

Page 31: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

31

12 3

6

4 5

0

1

3

20

2

Stack (before): Stack (after): 1, 6

Answer:

7

8

Topo Sort w/ stack

1

1

Page 32: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

32

12 3

6

4 5

0

1

3

20

2

Stack (before): 1, 6Stack (after): 1, 7, 8

Answer: 6

7

80

0

Topo Sort w/ stack

Page 33: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

33

12 3

6

4 5

0

1

3

20

2

Stack (before): 1, 7, 8Stack (after): 1, 7

Answer: 6, 8

7

80

0

Topo Sort w/ stack

Page 34: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

34

12 3

6

4 5

0

1

3

20

2

Stack (before): 1, 7Stack (after): 1

Answer: 6, 8, 7

7

80

0

Topo Sort w/ stack

Page 35: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

35

12 3

6

4 5

0

0

2

10

2

Stack (before): 1Stack (after): 2

Answer: 6, 8, 7, 1

7

80

0

Topo Sort w/ stack

Page 36: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

36

12 3

6

4 5

0

0

1

00

2

Stack (before): 2Stack (after): 3

Answer: 6, 8, 7, 1, 2

7

80

0

Topo Sort w/ stack

Page 37: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

37

12 3

6

4 5

0

0

0

00

1

Stack (before): 3Stack (after): 4

Answer: 6, 8, 7, 1, 2, 3

7

80

0

Topo Sort w/ stack

Page 38: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

38

12 3

6

4 5

0

0

0

00

0

Stack (before): 4Stack (after): 5

Answer: 6, 8, 7, 1, 2, 3, 4

7

80

0

Topo Sort w/ stack

Page 39: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

39

12 3

6

4 5

0

0

0

00

0

Stack (before): 5Stack (after):

Answer: 6, 8, 7, 1, 2, 3, 4, 5

7

80

0

Topo Sort w/ stack

Page 40: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

40

12 3

4 5

0

1

2

2

1

Queue (before): Queue (after): 1

Answer:

TopoSort Fails (cycle)

Page 41: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

41

12 3

4 5

0

0

1

2

1

Queue (before): 1Queue (after): 2

Answer: 1

TopoSort Fails (cycle)

Page 42: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

42

12 3

4 5

0

0

1

1

1

Queue (before): 2Queue (after):

Answer: 1, 2

TopoSort Fails (cycle)

Page 43: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

43

What is the run-time???

Initialize D // Mapping of vertex to its in-degreeQueue Q := [Vertices with in-degree 0]while notEmpty(Q) do x := Dequeue(Q) Output(x) y := A[x]; // y gets a linked list of vertices while y null do D[y.value] := D[y.value] – 1; if D[y.value] = 0 then Enqueue(Q,y.value); y := y.next; endwhileendwhile

Page 44: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

44

Topological Sort Analysis• Initialize In-Degree array: O(|V| + |E|)• Initialize Queue with In-Degree 0 vertices: O(|V|)• Dequeue and output vertex:

– |V| vertices, each takes only O(1) to dequeue and output: O(|V|)

• Reduce In-Degree of all vertices adjacent to a vertex and Enqueue any In-Degree 0 vertices:– O(|E|)

• For input graph G=(V,E) run time = O(|V| + |E|)– Linear time!

Page 45: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

Depth-first search

• depth-first search (DFS): finds a path between two vertices by exploring each possible path as many steps as possible before backtracking

– often implemented recursively

45

Page 46: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

DFS example• All DFS paths from A to others (assumes ABC edge order)

– A– A -> B– A -> B -> D– A -> B -> F– A -> B -> F -> E– A -> C– A -> C -> G

• What are the paths that DFS did not find?

46

Page 47: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

DFS pseudocode• Pseudo-code for depth-first search:

dfs(v1, v2): dfs(v1, v2, {}) dfs(v1, v2, path): path += v1. mark v1 as visited. if v1 is v2: path is found. for each unvisited neighbor vi of v1 where there is an edge from v1 to vi: if dfs(vi, v2, path) finds a path, path is found. path -= v1. path is not found.

47

Page 48: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

DFS observations

• guaranteed to find a path if one exists

• easy to retrieve exactly what the pathis (to remember the sequence of edgestaken) if we find it

• optimality: not optimal. DFS is guaranteed to find a path, not necessarily the best/shortest path– Example: DFS(A, E) may return

A -> B -> F -> E

48

Page 49: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

Another DFS example• Using DFS, find a path from BOS to LAX.

JFK

BOS

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

49

Page 50: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

Breadth-first search

• breadth-first search (BFS): finds a path between two nodes by taking one step down all paths and then immediately backtracking

– often implemented by maintaininga list or queue of vertices to visit

– BFS always returns the path with the fewest edges between the start and the goal vertices

50

Page 51: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

BFS example• All BFS paths from A to others (assumes ABC edge order)

– A– A -> B– A -> C– A -> E– A -> B -> D– A -> B -> F– A -> C -> G

• What are the paths that BFS did not find?

51

Page 52: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

52

BFS pseudocode• Pseudo-code for breadth-first search:

bfs(v1, v2): List := {v1}. mark v1 as visited.

while List not empty: v := List.removeFirst(). if v is v2: path is found.

for each unvisited neighbor vi of v where there is an edge from v to vi: List.addLast(vi).

path is not found.

Page 53: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

BFS observations• optimality:

– in unweighted graphs, optimal. (fewest edges = best)

– In weighted graphs, not optimal.(path with fewest edges might not have the lowest weight)

• disadvantage: harder to reconstruct what the actual path is once you find it– conceptually, BFS is exploring many possible paths in parallel, so it's

not easy to store a Path array/list in progress

• observation: any particular vertex is only part of one partial path at a time– We can keep track of the path by storing predecessors for each vertex

(references to the previous vertex in that path)

53

Page 54: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

Another BFS example• Using BFS, find a path from BOS to SFO.

JFK

BOS

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

54

Page 55: CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

DFS, BFS runtime• What is the expected runtime of DFS, in terms of the

number of vertices V and the number of edges E ?

• What is the expected runtime of BFS, in terms of the number of vertices V and the number of edges E ?

• Answer: O(|V| + |E|)– each algorithm must potentially visit every node and/or

examine every edge once.– why not O(|V| * |E|) ?

• What is the space complexity of each algorithm?55