65
1 Graphs and Graph Algorithms

1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

  • View
    215

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

1

Graphs and Graph Algorithms

Page 2: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

2

Graph Traversals

Depth First Traversal. Starting from a source node, mark the

node as visited Choose an ordering of the edges from

this node. Do a Depth First Traversal of the vertex

the first edge is connected to, provided the vertex has not yet been visited.

Repeat the previous step for all edges.

Page 3: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

3

Depth First Traversal From Vertex A

Assume ordering is alphabetical.From A, go to B.

A

B

C

D

E

F

2

1

3

74

3 7

6

2

6

Page 4: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

4

Depth First Traversal From Vertex A

From B, go to D.

A

B

C

D

E

F

2

1

3

74

3 7

6

2

6

Page 5: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

5

Depth First Traversal From Vertex A

From D, go to C.

A

B

C

D

E

F

2

1

3

74

3 7

6

2

6

Page 6: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

6

Depth First Traversal From Vertex A

From C, go to E.

A

B

C

D

E

F

2

1

3

74

3 7

6

2

6

Page 7: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

7

Depth First Traversal From Vertex A

From E, go to F.

A

B

C

D

E

F

2

1

3

74

3 7

6

2

6

Page 8: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

8

Depth First Traversal From Vertex A

Done.

A

B

C

D

E

F

2

1

3

74

3 7

6

2

6

Page 9: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

9

Breadth First Traversal

Mark the source vertex as visitedVisit all the children of the source

(provided they have not already been visited)

Perform a Breadth First Traversal of all of the children.

Page 10: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

10

Breadth First Traversal From Vertex A

Assume ordering is alphabetical.From A, go to B.

A

B

C

D

E

F

2

1

3

74

3 7

6

2

6

Page 11: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

11

Breadth First Traversal From Vertex A

From A, go to C.

A

B

C

D

E

F

2

1

3

74

3 7

6

2

6

Page 12: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

12

Breadth First Traversal From Vertex A

From B, go to D.

A

B

C

D

E

F

2

1

3

74

3 7

6

2

6

Page 13: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

13

Breadth First Traversal From Vertex A

From B, go to E.

A

B

C

D

E

F

2

1

3

74

3 7

6

2

6

Page 14: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

14

Breadth First Traversal From Vertex A

From B, go to F.

A

B

C

D

E

F

2

1

3

74

3 7

6

2

6

Page 15: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

15

Breadth First Traversal From Vertex A

Done.

A

B

C

D

E

F

2

1

3

74

3 7

6

2

6

Page 16: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

16

Definitions of Connectivity

An undirected graph is connected if there is a path from v to w for all vertices v and w in the vertex set.

A directed graph is strongly connected if there is a path from v to w for all vertices v and w in the vertex set.

A directed graph is weakly connected if its underlying undirected graph is connected.

Page 17: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

17

Underlying Undirected Graphs

Every directed graph has an underlying undirected graph.

This graph is found as follows: It has the same vertices as the directed graph If the directed graph has an edge (v,w), then

the underlying undirected graph has edges (v,w) and (w,v).

Simply put, to find the underlying undirected graph, remove the arrows.

Page 18: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

18

Testing for Connectivity

For undirected graphs: Apply a DFT or a BFT from any vertex. If all

vertices are visited, the graph is connected.For weakly connected directed graphs:

Create the underlying undirected graph and test it for connectivity as above.

For strongly connected directed graphs: Apply a DFT or a BFT from every vertex. If all

vertices are reachable every time, the graph is strongly connected.

Page 19: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

19

Dijkstra’s Algorithm

Start with the Finished Set of Nodes containing only the source.

Find all nodes not in the Finished Set of Nodes, but connected to one of these nodes. Call this set Next Nodes.

Find the node in the set of Next Nodes which has minimum cost to link to the Finished Set of Nodes.

Add this node to the Finished Set of Nodes

Page 20: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

20

Dijkstra’s Algorithm II

Repeat the previous 3 steps until all nodes are added to the Finished Set of Nodes.

Note: To determine the minimum cost to link a node: Let PrevNode be the one currently in the

Finished set of Nodes; Let ThisNextNode be the one it is linked to. The cost of ThisNextNode = cost of PrevNode

+ cost of the link between the two.

Page 21: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

21

Dijkstra’s Algorithm Example

Suppose A wishes to send to F. What is the cheapest path?

A

B

C

D

E

F

2

1

3

74

3 7

6

2

6

Page 22: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

22

Dijkstra’s Algorithm Example

Tan nodes are in the Finished Set.Blue nodes are in the Next Set.

A

B

C

D

E

F

2

1

3

74

3 7

6

2

60

2

1

Page 23: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

23

Dijkstra’s Algorithm Example

What happens next?

A

B

C

D

E

F

2

1

3

74

3 7

6

2

60

2

1

Page 24: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

24

Dijkstra’s Algorithm Example

C is least cost among Next Node set, so it is added to Finished set.

A

B

C

D

E

F

2

1

3

74

3 7

6

2

60

2

1

How do I update the Next Node Set?

Page 25: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

25

Dijkstra’s Algorithm Example

D, E, F added to Next Node Set. What are their costs?

A

B

C

D

E

F

2

1

3

74

3 7

6

2

60

2

1

4

7

8

What is next for Finished Set of nodes?

Page 26: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

26

Dijkstra’s Algorithm Example

B is added to Finished set of nodes.

A

B

C

D

E

F

2

1

3

74

3 7

6

2

60

2

1

4

7

8

How do I update the Next Set of Nodes?

Page 27: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

27

Dijkstra’s Algorithm Example

E’s minimum changes to 6.

A

B

C

D

E

F

2

1

3

74

3 7

6

2

60

2

1

4

6

8

Next for Finished Set?

Page 28: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

28

Dijkstra’s Algorithm Example

Next, update Next Set costs:

A

B

C

D

E

F

2

1

3

74

3 7

6

2

60

2

1

4

6

8

D has minimum cost.

Page 29: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

29

Dijkstra’s Algorithm Example

Next for Finished Set?

A

B

C

D

E

F

2

1

3

74

3 7

6

2

60

2

1

4

6

6

Page 30: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

30

Dijkstra’s Algorithm Example

No cost updates for Next Nodes here; finish it up:

A

B

C

D

E

F

2

1

3

74

3 7

6

2

60

2

1

4

6

6

Either E or F; I chose E.

Page 31: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

31

Dijkstra’s Algorithm Example

All nodes in finished set...done

A

B

C

D

E

F

2

1

3

74

3 7

6

2

60

2

1

4

6

6

Page 32: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

32

Dijkstra Discussion

This example is centralized; can we make it distributed? Each node would need the entire graph

before starting. Each node would need to send its

connection costs to everyone else.Can we make it adaptive?

When costs change, they would need to be sent to everyone and the algorithm rerun.

Page 33: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

33

Bellman-Ford Algorithm

Basic idea: Keep track of current minimum cost to

all other nodes . When the minimum cost to another

node from one of your neighbors changes, maybe you can change your cost to that node too.

Initially, you only know the cost to your neighbors.

Page 34: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

34

Bellman-Ford Explanation

Thus, the minimum cost from A to F is the minimum of: The cost from A to B plus the cost from B to F; The cost from A to C plus the cost from C to F.

Thus, each source node can fill in its destination minimums by looking at its neighbor’s minimums.

This process iterates until no changes are made to the list of minimums.

Page 35: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

35

Bellman-Ford Example I

This is the same graph as for Dijkstra’s Algorithm and the initial table.

A

B

C

D

E

F

2

1

37

4

3 7

6

2

6

A B C D E FA NA B,2 C,1 ? ? ?B A,2 NA ? D,3 E,4 F,7C A,1 ? NA D,3 E,6 F,7D ? B,3 C,3 NA ? F,2E ? B,4 C,6 ? NA F,6F ? B,7 C,7 D,2 E,6 NA

Page 36: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

36

Bellman-Ford Example II

A B C D E FA NA B,2 C,1 ? ? ?B A,2 NA ? D,3 E,4 F,7C A,1 ? NA D,3 E,6 F,7D ? B,3 C,3 NA ? F,2E ? B,4 C,6 ? NA F,6F ? B,7 C,7 D,2 E,6 NA

A B C D E FA NA B,2 C,1 C,4 B,6 C,8B A,2 NA A,3 D,3 E,4 D,5C A,1 A,3 NA D,3 E,6 D,5D C,4 B,3 C,3 NA B,7 F,2E B,6 B,4 C,6 B,7 NA F,6F C,8 D,5 D,5 D,2 E,6 NA

First Iteration (Repeated):

Second Iteration:

Page 37: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

37

A B C D E FA NA B,2 C,1 C,4 B,6 C,8B A,2 NA A,3 D,3 E,4 D,5C A,1 A,3 NA D,3 E,6 D,5D C,4 B,3 C,3 NA B,7 F,2E B,6 B,4 C,6 B,7 NA F,6F C,8 D,5 D,5 D,2 E,6 NA

Bellman-Ford Example III

Second Iteration (Repeated):

Third Iteration:A B C D E F

A NA B,2 C,1 C,4 B,6 C,6B A,2 NA A,3 D,3 E,4 D,5C A,1 A,3 NA D,3 E,6 D,5D C,4 B,3 C,3 NA B,7 F,2E B,6 B,4 C,6 B,7 NA F,6F D,6 D,5 D,5 D,2 E,6 NA

Page 38: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

38

Bellman-Ford Discussion

This example is centralized; can we make it distributed? Each node needs to send its costs to its

neighbors only. In our example, A only needs the rows

for B and C to decide on its cost to all destinations.

Page 39: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

39

Bellman-Ford Discussion II

Can we make it adaptive? Each node keeps the costs from its

neighbors; when these costs change, it only needs to pick the minimum among the old costs and the new one.

NOTE: when a cost changes, a node must notify its neighbors.

Page 40: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

40

Minimum Cost Spanning Trees

Prim’s Algorithm May be viewed as a “vertex-centric”

approach. Basically the same as Dijkstra’s except that

the cost of a node is its minimum cost connection to the finished set, rather than minimum connection to source vertex.

Proceed with adding the minimum vertex to the finished set until all vertices are added.

Page 41: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

41

Prim’s Algorithm Example

Same graph as for Dijkstra’s Algorithm, but let’s start at D.

A

B

C

D

E

F

2

1

3

74

3 7

6

2

6

Page 42: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

42

Prim’s Algorithm Example

Next for Finished Set?

A

B

C

D

E

F

2

1

3

74

3 7

6

2

6

3

3

0

2

Page 43: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

43

Prim’s Algorithm Example

Insert F.Next for Finished Set?

A

B

C

D

E

F

2

1

3

74

3 7

6

2

6

3

3

0

2

6

Page 44: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

44

Prim’s Algorithm Example

Insert B. Note change of E’s weight.Next for Finished Set?

A

B

C

D

E

F

2

1

3

74

3 7

6

2

6

3

3

0

2

4

2

Page 45: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

45

Prim’s Algorithm Example

Insert A. Note change of C’s weight.Next for Finished Set?

A

B

C

D

E

F

2

1

3

74

3 7

6

2

6

3

1

0

2

4

2

Page 46: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

46

Prim’s Algorithm Example

Insert C. Next for Finished Set?

A

B

C

D

E

F

2

1

3

74

3 7

6

2

6

3

1

0

2

4

2

Page 47: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

47

Prim’s Algorithm Example

Insert E. Done.

A

B

C

D

E

F

2

1

3

74

3 7

6

2

6

3

1

0

2

4

2

Page 48: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

48

Prim’s Discussion

This is not the same minimum spanning tree as produced by Dijkstra’s Algorithm.

Starting from node A, we added (C,D) rather than (B,D).

Both edges have the same weight, so the overall cost is the same.

This means that there may be more than one minimum spanning tree!

Page 49: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

49

Kruskal’s Algorithm

More of an “edge-centric” approach.Idea: sort the edges; add edges from

least weight to maximum weight with the restriction that the newly added edge does not create a cycle.

Page 50: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

50

Kruskal’s Algorithm Example

Which edge has minimum weight?

A

B

C

D

E

F

2

1

3

74

3 7

6

2

6

Page 51: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

51

Kruskal’s Algorithm Example

Edge (A,C) is minimum. What’s next?

A

B

C

D

E

F

2

1

3

74

3 7

6

2

6

Page 52: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

52

Kruskal’s Algorithm Example

Edge (A,B) is minimum. What’s next?

A

B

C

D

E

F

2

1

3

74

3 7

6

2

6

Page 53: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

53

Kruskal’s Algorithm Example

Edge (D,F) is minimum. What’s next?

A

B

C

D

E

F

2

1

3

74

3 7

6

2

6

Page 54: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

54

Kruskal’s Algorithm Example

Edge (B,D) is minimum. What’s next?

A

B

C

D

E

F

2

1

3

74

3 7

6

2

6

Page 55: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

55

Kruskal’s Algorithm Example

Edge (C,D) is minimum, but creates a cycle. Here, we choose (B,E). What’s next?

A

B

C

D

E

F

2

1

3

74

3 7

6

2

6

Page 56: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

56

Kruskal’s Algorithm Example

All vertices are now in the tree, so we are done.

A

B

C

D

E

F

2

1

3

74

3 7

6

2

6

Page 57: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

57

Kruskal’s Algorithm -- Detecting Cycles

Neat algorithm, but how are cycles detected?

Answer: Start by considering each vertex in its

own distinct set. Whenever an edge (v,w) is considered, v

and w must be in different vertex sets. When adding edge (v,w), merge the sets

containing v and w into one set.

Page 58: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

58

Kruskal’s Algorithm Example with edge sets

Vertex Sets: ({A},{B},{C},{D},{E},{F}}

A

B

C

D

E

F

2

1

3

74

3 7

6

2

6

Page 59: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

59

Kruskal’s Algorithm Example

Vertex Sets: ({A,C},{B},{D},{E},{F}}

A

B

C

D

E

F

2

1

3

74

3 7

6

2

6

Page 60: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

60

Kruskal’s Algorithm Example

Vertex Sets: ({A,B,C},{D},{E},{F}}

A

B

C

D

E

F

2

1

3

74

3 7

6

2

6

Page 61: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

61

Kruskal’s Algorithm Example

Vertex Sets: ({A,B,C},{D,F},{E}}

A

B

C

D

E

F

2

1

3

74

3 7

6

2

6

Page 62: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

62

Kruskal’s Algorithm Example

Vertex Sets: ({A,B,C,D,F},{E}}

A

B

C

D

E

F

2

1

3

74

3 7

6

2

6

Page 63: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

63

Kruskal’s Algorithm Example

Note that (C,D) is minimum, but C and D are in the same vertex set.

Vertex Sets: ({A,B,C,D,F},{E}}

A

B

C

D

E

F

2

1

3

74

3 7

6

2

6

Page 64: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

64

Kruskal’s Algorithm Example

Vertex Sets: ({A,B,C,D,E,F}}All vertices are now in one set, so

we are done.

A

B

C

D

E

F

2

1

3

74

3 7

6

2

6

Page 65: 1 Graphs and Graph Algorithms. 2 Graph Traversals zDepth First Traversal. yStarting from a source node, mark the node as visited yChoose an ordering of

65

The End Slide