chapter 20stornar/courses/notes/cs240/20.graphs.pdf · path • a sequence of edges that begins at...

135
Chapter 20 Graphs 1

Upload: others

Post on 20-Jan-2021

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Chapter 20

Graphs

1

Page 2: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Graph

• Set V of vertices and E of edges

2

Page 3: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Subgraph

• Subset of vertices and edges

3

(a) A campus map as a graph; (b) a subgraph

Page 4: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Adjacent vertices

• Joined by an edge– Library-Dormitory– Library-Student Union

4

Page 5: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Path

• A sequence of edges that begins at one vertex and ends at another vertex

• May pass through the same vertex more than once

5

Page 6: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Simple path

• A path that passes through a vertex only once

6

Page 7: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Cycle

• A path that begins and ends at the same vertex– Library-Dormitory-Student

Union-Gymnasium-Student Union-Library

7

Page 8: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Simple cycle

• A cycle that does not pass through a vertex more than once– Library-Dormitory-Student

Union-Library

8

Page 9: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Connected graph

• A graph that has a path between each pair of distinct vertices

9

Page 10: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Disconnected graph

• A graph that has at least one pair of vertices without a path between them

10

Page 11: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Complete graph

• A graph that has an edge between each pair of distinct vertices

11

Page 12: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Weighted graph

• A graph whose edges have numeric labels

12

Page 13: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Undirected graph

• Edges do not indicate a direction

13

Page 14: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Directed graph

• Each edge is a directed edge– Vertex y is adjacent to vertex x if there is

a directed edge from x to y

14

Page 15: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Graphs as ADTs• Vertices may or may not contain values

• A graph may have directed or undirected edges

• Insertion/deletion operations apply to vertices and edges

• Graphs may have traversal operations

15

Page 16: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Adjacency Matrix - Unweighted

• n vertices numbered 0, 1, …, n – 1

• An n by n array matrix such that matrix[i][j] is

• 1 (or true) if there is an edge from vertex i to vertex j

• 0 (or false) if there is no edge from vertex i to vertex j

16

Page 17: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Adjacency Matrix Unweighted/Directed

17

z

Y

W S

TPR

X Q16

40

2

35

7

8

0 0 1 0 0 1 0 0 00 0 0 0 0 0 1 0 0

0 0 0 0 0 0 1 0 00 0 0 0 1 0 0 0 0

0 0 0 0 0 1 0 0 0

0 0 0 1 0 0 0 1 00 0 0 0 0 0 0 0 0

0 0 1 0 0 0 0 0 10 0 0 0 0 0 0 0 0

1 Q

0 P1 Q

6 X

2 R

7 Y

3 S4 T

8 Z

5 W

0 P

2 R

3 S

4 T

5 W

6 X

7 Y

8 Z

Directed Graph

Page 18: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Adjacency Matrix - Weighted

• n vertices numbered 0, 1, …, n – 1

• An n by n array matrix such that matrix[i][j] is– The weight that labels the edge from vertex i

to vertex j if there is an edge from i to j– ∞ if there is no edge from vertex i to vertex j

18

Page 19: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Adjacency Matrix Weighted/Undirected

19

Page 20: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Adjacency List - Directed

• n vertices numbered 0, 1, …, n – 1– Consists of n linked lists

– The ith linked list has a node for vertex j if and only if the graph contains an edge from vertex i to vertex j

– The node can contain either• Vertex j’s value, if any• An indication of vertex j’s identity

20

Page 21: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Adjacency ListUnweighted/Directed

21

Page 22: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Implementing Graphs

• Two common operations on graphs– Determine whether there is an edge from vertex i to

vertex j– Find all vertices adjacent to a given vertex i

• Adjacency matrix – Supports operation 1 more efficiently

• Adjacency list – Supports operation 2 more efficiently– Often requires less space than an adjacency matrix

22

Page 23: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Graph - Using the STL

• An adjacency list representation of a graph may be implemented with a vector of maps

• For a weighted graph– The vector elements represent the vertices of a

graph– The map for each vertex contains element pairs

• Each pair consists of an adjacent vertex and an edge weight

23

Page 24: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Graph - Using the STLclass Edge { public:

int v, w, weight;Edge(int firstVertex, int secondVertex, int edgeWeight) { v = firstVertex; w = secondVertex; weight = edgeWeight;}

};

class Graph { public:

int numVertices;int numEdges;vector< map<int, int> > adjList;Graph(int n);int GetNumVertices() const;int GetNumEdges() const;int GetWeight(Edge e) const;void Add(Edge e);void Remove(Edge e);map<int, int>::iterator FindEdge(int v, int w);

};

24

Page 25: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Graph Traversals

• Visits all vertices of the graph if and only if the graph is connected– A connected component - The subset of vertices

visited during a traversal that begins at a given vertex

• To prevent indefinite loops• Mark each vertex during a visit, and • Never visit a vertex more than once

25

Page 26: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

DFS Traversal

• Depth-First Search (DFS) Traversal

– Proceeds along a path from a vertex v as deeply into the graph as possible before backing up

– A last visited, first explored strategy

– Has a simple recursive form

– Has an iterative form that uses a stack

26

Page 27: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

DFS :: Recursive

dfs(v: Vertex) {Mark v as visitedfor (each unvisited vertex u adjacent to v) {

dfs(u)} // end for

} // end dfs()

27

Page 28: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

DFS :: Iterativedfs(v: Vertex) {

s = a new empty stack

// Push v onto the stack and mark its.push(v)Mark v as visitedwhile (!s.isEmpty()) { if (all adjacent nodes of top vertex have been visited)

pop()else {

Select an unvisited vertex u adjacent to the vertex on the top of the stackpush(u)Mark u as visited

} // end if} // end while

} // end dfs()

28

Page 29: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

DFS:A

29

A

DG

H

I

C

B

F

E

Page 30: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

DFS:AB

30

A

DG

H

I

C

B

F

E

Page 31: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

DFS:ABE

31

A

DG

H

I

C

B

F

E

Page 32: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

DFS:ABEC

32

A

DG

H

I

C

B

F

E

Page 33: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

DFS:ABECD

33

A

DG

H

I

C

B

F

E

Page 34: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

DFS:ABECDH

34

A

DG

H

I

C

B

F

E

Page 35: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

DFS:ABECDHG

35

A

DG

H

I

C

B

F

E

Page 36: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

DFS:ABECDHGI

36

A

DG

H

I

C

B

F

E

Page 37: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

DFS:ABECDHGIF

37

A

DG

H

I

C

B

F

E

Page 38: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

BFS Traversal

• Breadth-First Search (BFS) Traversal

– Visits every vertex adjacent to a vertex v that it can before visiting any other vertex

– A first visited, first explored strategy

– An iterative form uses a queue

– A recursive form is possible, but not simple

38

Page 39: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

BFS :: Iterativebfs(v: Vertex) {

q = a new empty queue

// Add v to the queue and mark itq.add(v)Mark v as visited

while (!q.isEmpty()) {q.remove(w)for(each unvisited vertex u adjacent to w) {

Mark u as visitedq.add(u)

} // end for} // end while

} // end bfs()

39

Page 40: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

BFS:A

40

A

DG

H

I

C

B

F

E

Page 41: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

BFS:ABC

41

A

DG

H

I

C

B

F

E

Page 42: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

BFS:ABCE

42

A

DG

H

I

C

B

F

E

Page 43: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

BFS:ABCEDI

43

A

DG

H

I

C

B

F

E

Page 44: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

BFS:ABCEDI

44

A

DG

H

I

C

B

F

E

Page 45: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

BFS:ABCEDIH

45

A

DG

H

I

C

B

F

E

Page 46: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

BFS:ABCEDIHF

46

A

DG

H

I

C

B

F

E

Page 47: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

BFS:ABCEDIHFG

47

A

DG

H

I

C

B

F

E

Page 48: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

BFS - Using the STL

• A BFS class can be implemented with the STL vector and queue containers

• Two vectors of integers– mark stores vertices that have been visited– parents stores the parent of each vertex for use by

other graph algorithms.

• A queue of Edges – BFS processes the edges from each vertex’s adjacency

list in the order that they were pushed onto the queue

48

Page 49: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Graph ApplicationsTopological Sorting

• Topological order– A list of vertices in a directed graph without

cycles such that vertex x precedes vertex y if there is a directed edge from x to y in the graph

– There may be several topological orders in a given graph

• Topological sorting– Arranging the vertices into a topological order

49

Page 50: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Topological Sorting

50

A directed graph without

cycles

The graph above arranged

according to the topological

orders a) a, g, d, b, e, c, f and

b) a, b, g, d, e, f, c

Page 51: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

'backward' topo sort

• Find a vertex that has no successor

• Remove that vertex and all edges that lead to it, and add the vertex to the beginning of a list of vertices

• Add each subsequent vertex that has no successor to the beginning of the list

51

Page 52: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

'backward' topo sort

topSort1(in theGraph:Graph, out aList:List)

n = number of vertices in theGraph for (step = 1 through n) { Select a vertex v that has no successors aList.insert(1,v) Delete from theGraph vertex v and its edges}

52

Page 53: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

'backward' topo sort

53

a b c

fed

g

Page 54: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

'backward' topo sort

54

a b

fed

g

L: c

Page 55: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

'backward' topo sort

55

a b

ed

g

L: f c

Page 56: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

'backward' topo sort

56

a b

ed

g

L: f c

Page 57: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

'backward' topo sort

57

a b

d

g

L: e f c

Page 58: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

'backward' topo sort

58

a b

d

g

L: e f c

Page 59: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

'backward' topo sort

59

a

d

g

L: b e f c

Page 60: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

'backward' topo sort

60

a

g

L: d b e f c

Page 61: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

'backward' topo sort

61

g

L: a d b e f c

Page 62: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

'backward' topo sort

62

g

L: a d b e f c

Page 63: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

'backward' topo sort

63

L: g a d b e f c

Page 64: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

'forward' topo sort

• A modification of the iterative DFS algorithm

• Push all vertices that have no predecessor onto a stack

• Each time you pop a vertex from the stack, add it to the beginning of a list of vertices

• When the traversal ends, the list of vertices will be in topological order

64

Page 65: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

'forward' topo sort

topSort2(in theGraph:Graph, out aList:List)

s.createStack() for (all vertices in the graph){ if (v has no predecessors) { s.push(v) Mark v as visited }

} while (!s.isEmpty()) { if (v has unvisited adjacent nodes) { Select an unvisited vertex u adjacent to v

s.push(u) Mark u as visited

} else { s.pop(v) aList.insert(1, v) } }

65

Page 66: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

'forward' topo sortS:

66

c

f

a b

ed

g

Page 67: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

'forward' topo sortS: a

67

c

f

a b

ed

g

Page 68: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

'forward' topo sortS: a g

68

c

f

a b

ed

g

Page 69: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

'forward' topo sortS: a g

69

c

f

a b

ed

g

Page 70: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

'forward' topo sortS: a g d

70

c

f

a b

ed

g

Page 71: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

'forward' topo sortS: a g d

71

c

f

a b

ed

g

Page 72: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

'forward' topo sortS: a g d e

72

c

f

a b

ed

g

Page 73: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

'forward' topo sortS: a g d e

73

c

f

a b

ed

g

Page 74: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

'forward' topo sortS: a g d e c

74

c

f

a b

ed

g

Page 75: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

'forward' topo sortS: a g d e c f

75

c

f

a b

ed

g

Page 76: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

'forward' topo sortS: a g d e c

76

c

f

a b

ed

g

L: f

Page 77: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

'forward' topo sortS: a g d e

77

c

f

a b

ed

g

L: c f

Page 78: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

'forward' topo sortS: a g d

78

c

f

a b

ed

g

L: e c f

Page 79: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

'forward' topo sortS: a g

79

c

f

a b

ed

g

L: d e c f

Page 80: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

'forward' topo sortS: a

80

c

f

a b

ed

g

L: g d e c f

Page 81: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

'forward' topo sortS: a

81

c

f

a b

ed

g

L: g d e c f

Page 82: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

'forward' topo sortS: a b

82

c

f

a b

ed

g

L: g d e c f

Page 83: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

'forward' topo sortS: a

83

c

f

a b

ed

g

L: b g d e c f

Page 84: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

'forward' topo sortS:

84

c

f

a b

ed

g

L: a b g d e c f

Page 85: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Spanning Trees

• A connected undirected subgraph of G that contains all of G’s vertices and enough of its edges to form a tree

85

Page 86: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Spanning Trees

Remove edges until there are no cycles

86

A connected graph with cycles

A spanning tree for the graph

Page 87: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Spanning Trees

• Detecting a cycle in an undirected connected graph– A connected undirected graph that has n vertices

must have at least n – 1 edges

– A connected undirected graph that has n vertices and exactly n – 1 edges cannot contain a cycle

– A connected undirected graph that has n vertices and more than n – 1 edges must contain at least one cycle

87

Page 88: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Spanning Trees

• Connected graphs that each have four vertices and three edges

88

Page 89: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

The DFS Spanning Tree

• To create a depth-first search (DFS) spanning tree– Traverse the graph using a depth-first

search and mark the edges that you follow

– After the traversal is complete, the graph’s marked vertices and edges form the spanning tree

89

Page 90: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

The DFS Spanning Tree

dfsTree(in v:Vertex)

Mark v as visited for (each unvisited vertex u adjacent to v) { Mark the edge from u to v dfsTree(u) }

90

Page 91: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

DFS:A

91

A

DG

H

I

C

B

F

E

Page 92: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

DFS:AB

92

A

DG

H

I

C

B

F

E

Page 93: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

DFS:ABE

93

A

DG

H

I

C

B

F

E

Page 94: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

DFS:ABEC

94

A

DG

H

I

C

B

F

E

Page 95: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

DFS:ABECD

95

A

DG

H

I

C

B

F

E

Page 96: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

DFS:ABECDH

96

A

DG

H

I

C

B

F

E

Page 97: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

DFS:ABECDHG

97

A

DG

H

I

C

B

F

E

Page 98: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

DFS:ABECDHGI

98

A

DG

H

I

C

B

F

E

Page 99: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

DFS:ABECDHGIF

99

A

DG

H

I

C

B

F

E

Page 100: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

DFST:ABECDHGIF

100

A

DG

H

I

C

B

F

E

Page 101: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

The BFS Spanning Tree

• To create a breath-first search (BFS) spanning tree– Traverse the graph using a bread-first

search and mark the edges that you follow

– When the traversal is complete, the graph’s marked vertices and edges form the spanning tree

101

Page 102: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

The BFS Spanning TreebfsTree(in v:Vertex)

q.createQueue() q.enqueue(v) Mark v as visited while(!q.isEmpty()) { d.dequeue(w) for (each unvisited vertex u adjacent to w) { Mark u as visited Mark edge between w and u q.enqueue(u) } }

102

Page 103: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

BFS:A

103

A

DG

H

I

C

B

F

E

Page 104: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

BFS:ABC

104

A

DG

H

I

C

B

F

E

Page 105: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

BFS:ABCE

105

A

DG

H

I

C

B

F

E

Page 106: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

BFS:ABCEDI

106

A

DG

H

I

C

B

F

E

Page 107: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

BFS:ABCEDI

107

A

DG

H

I

C

B

F

E

Page 108: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

BFS:ABCEDIH

108

A

DG

H

I

C

B

F

E

Page 109: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

BFS:ABCEDIHF

109

A

DG

H

I

C

B

F

E

Page 110: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

BFS:ABCEDIHFG

110

A

DG

H

I

C

B

F

E

Page 111: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

BFST:ABCEDIHFG

111

A

DG

H

I

C

B

F

E

Page 112: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Minimum Spanning Trees

• Sum of all edge costs is minimum

• There may be several minimum spanning trees for a particular graph

112

Page 113: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Prim's Algorithm

• Finds a minimal spanning tree that begins at any vertex– Find the least-cost edge (v, u) from a visited vertex v

to some unvisited vertex u

– Mark u as visited

– Add the vertex u and the edge (v, u) to the minimum spanning tree

– Repeat the above steps until there are no more unvisited vertices

113

Page 114: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

MST:A

114

A

DG

H

I

C

B

F

E

12

3

1

54

3

3

2

3

Page 115: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

MST:AB

115

A

DG

H

I

C

B

F

E

12

3

1

54

3

3

2

3

Page 116: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

MST:ABE

116

A

DG

H

I

C

B

F

E

12

3

1

54

3

3

2

3

Page 117: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

MST:ABEC

117

A

DG

H

I

C

B

F

E

12

3

1

54

3

3

2

3

Page 118: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

MST:ABECD

118

A

DG

H

I

C

B

F

E

12

3

1

54

3

3

2

3

Page 119: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

MST:ABECDI

119

A

DG

H

I

C

B

F

E

12

3

1

54

3

3

2

3

Page 120: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

MST:ABECDIF

120

A

DG

H

I

C

B

F

E

12

3

1

54

3

3

2

3

Page 121: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

MST:ABECDIFH

121

A

DG

H

I

C

B

F

E

12

3

1

54

3

3

2

3

Page 122: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

MST:ABECIDFHG

122

A

DG

H

I

C

B

F

E

12

3

1

54

3

3

2

3

Page 123: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Dijkstra's

• Finds the shortest path between a given origin and all other vertices

• vertexSet stores vertices along the path

• weight[v] is the weight of the shortest (cheapest) path from vertex 0 to vertex v that passes through vertices in vertexSet

123

Page 124: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Dijkstra

124

0 1

3 4

2Origin

8 21

1

7

94

3

2

Step v set w[0] w[1] w[2] w[3] w[4]

1 - 0 0 8 - 9 4

First row of adjacency matrix

Page 125: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Dijkstra

125

Step v set w[0] w[1] w[2] w[3] w[4]

1 - 0 0 8 - 9 4

0 1

3 4

2Origin

8 21

1

7

94

3

2

min wt

Page 126: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Dijkstra

126

Step v set w[0] w[1] w[2] w[3] w[4]

1 - 0 0 8 - 9 4

2 4 0,4 0 8 5 9 4

0 1

3 4

2Origin

8 21

1

7

94

3

2

Page 127: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Dijkstra

127

2

Step v set w[0] w[1] w[2] w[3] w[4]

1 - 0 0 8 - 9 4

2 4 0,4 0 8 5 9 4

0 1

3 4

2Origin

8 21

1

7

94

3

2

Page 128: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Dijkstra

128

Step v set w[0] w[1] w[2] w[3] w[4]

1 - 0 0 8 - 9 4

2 4 0,4 0 8 5 9 4

3 2 0,4,2 0 7 5 8 4

0 1

3 4

2Origin

8 21

1

7

94

3

2

Page 129: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Dijkstra

129

Step v set w[0] w[1] w[2] w[3] w[4]

1 - 0 0 8 - 9 4

2 4 0,4 0 8 5 9 4

3 2 0,4,2 0 7 5 8 4

0 1

3 4

2Origin

8 21

1

7

94

3

2

Page 130: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Dijkstra

130

Step v set w[0] w[1] w[2] w[3] w[4]

1 - 0 0 8 - 9 4

2 4 0,4 0 8 5 9 4

3 2 0,4,2 0 7 5 8 4

4 1 0,4,2,1 0 7 5 8 4

0 1

3 4

2Origin

8 21

1

7

94

3

2

Page 131: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Dijkstra

131

Step v set w[0] w[1] w[2] w[3] w[4]

1 - 0 0 8 - 9 4

2 4 0,4 0 8 5 9 4

3 2 0,4,2 0 7 5 8 4

4 1 0,4,2,1 0 7 5 8 4

0 1

3 4

2Origin

8 21

1

7

94

3

2

Page 132: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Dijkstra

132

Step v set w[0] w[1] w[2] w[3] w[4]

1 - 0 0 8 - 9 4

2 4 0,4 0 8 5 9 4

3 2 0,4,2 0 7 5 8 4

4 1 0,4,2,1 0 7 5 8 4

5 3 0,4,2,1,3 0 7 5 8 4

0 1

3 4

2Origin

8 21

1

7

94

3

2

Page 133: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Summary

• The two most common implementations of a graph are the adjacency matrix and the adjacency list

• Graph searching– Depth-first search goes as deep into the graph as

it can before backtracking– Bread-first search visits all possible adjacent

vertices before traversing further into the graph

133

Page 134: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Summary

• Topological sorting produces a linear order of the vertices in a directed graph without cycles

• Trees are connected undirected graphs without cycles

• A spanning tree of a connected undirected graph– A subgraph that contains all the graph’s vertices and

enough of its edges to form a tree

134

Page 135: Chapter 20stornar/courses/notes/cs240/20.Graphs.pdf · Path • A sequence of edges that begins at one vertex and ends at another vertex • May pass through the same vertex more

Summary

• A minimum spanning tree for a weighted undirected graph– A spanning tree whose edge-weight sum is

minimal

• The shortest path between two vertices in a weighted directed graph– The path that has the smallest sum of its edge

weights

135