Top Banner
Graphs I Kruse and Ryba Chapter 12
109

Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Graphs I

Kruse and Ryba

Chapter 12

Page 2: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Undirected Graphs

An undirected graph is a set of nodes and a set of edges between nodes; e.g.:

The graph is undirected because its edges do not have a particular direction.

Page 3: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Edges

We sometimes need to refer to the edges ei of a graph:

e0

e1

e3

e4

e5

e2

Page 4: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Vertices

We also need to refer to the vertices vj of a graph:

v1

e0

e1

e3

e4

e5

e2v2

v3

v4

v0

Page 5: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Undirected Graph Definition

• An undirected graph G = (V, E) is a finite set of vertices V, together with a finite set of edges E.

• Both sets might be empty (no vertices and no edges), which is called the empty graph.

• Each edge has associated with it two vertices.• If two vertices are connected by an edge, they are

said to be adjacent.

Page 6: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Undirected Graph Example: Subway Map

Page 7: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Another Undirected Graph: vBNS (fast internet backbone)

Page 8: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Directed Graphs

In a directed graph each vertex has an associated direction; e.g.:

Each edge is associated with two vertices: its source vertex and target vertex.

Page 9: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Directed Graph Examples

• Flow graph: vertices are pumping stations and edges are pipelines connecting them.

• Task network: each project has a number of component activities called tasks. Each task has a duration (amount of time needed to complete task).

Page 10: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Loops

A loop is an edge that connects a vertex with itself; for example:

Loops can occur in directed graphs or undirected graphs.

Page 11: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Multiple Edges

In principle, a graph may have two or more edges connecting the same two vertices in the same direction:

These are called multiple edges. In a graph diagram, each edge is drawn separately.

Page 12: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Simple Graphs

A graph is said to be a simple graph, if:1. the graph has no loops, and

2. no multiple edges

Page 13: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Path

• A path in a graph is a sequence of vertices, v0, v1, … vm, such that each adjacent pair of vertices in the list are connected by an edge.

v1

v2

v3

v4

v0

Page 14: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Path

• A path in a graph is a sequence of vertices, v0, v1, … vm, such that each adjacent pair of vertices in the list are connected by an edge.

v1

v2

v3

v4

v0Example path:

v0, v3, v4

Page 15: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Path

• A path in a graph is a sequence of vertices, v0, v1, … vm, such that each adjacent pair of vertices in the list are connected by an edge.

v1

v2

v3

v4

v0Example path:

v0, v3, v4

v0, v3, v2, v1, v4

Page 16: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Path

• In a directed graph, the connection must go from source vi to target vi+1.

v1

v2

v3

v4

v0Is there a path from

v0 to v4?v3 to v1? v1 to v2? v2 to v1?

Page 17: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Example Task Network

Start

Find a realtor

Look at homes

Choose a home to buy

Apply for financing and

establish price limit

Investigateneighborhood

schools

Prepare offer

Investigatetermite and soil

reports

Presentoffer

0

20

5 9

2

15

10

3

2

How long will it take to buy a house? (The duration of each task isshown below each task).

Page 18: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Critical Path

Start

Find a realtor

Look at homes

Choose a home to buy

Apply for financing and

establish price limit

Investigateneighborhood

schools

Prepare offer

Investigatetermite and soil

reports

Presentoffer

0

20

5 9

2

15

10

3

2

The task will take as long as the maximum duration path from start to finish.

Page 19: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Cycles

• A cycle in a graph is a path that leads back to the first node in the path.

v1

v2

v3

v4

v0Example cycles:

v1, v2, v3, v2, v1

Page 20: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Cycles

• A cycle in a graph is a path that leads back to the first node in the path.

v1

v2

v3

v4

v0Example cycles:

v1, v2, v3, v2, v1

v0, v3, v4, v1, v0

Page 21: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Does this graph have any cycles?

Page 22: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Cycles in Directed Graphs

A cycle in a directed graph must follow edge directions; e.g.:

Page 23: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

DAG: Directed Acyclic GraphA directed acyclic graph (DAG) must have no cycles;

e.g.:

In other words, there is no path that starts and ends at the same node.

Page 24: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Complete Graph

An undirected graph which has an edge between every pair of vertices.

Page 25: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Connected Graph

An undirected graph which has a path between every pair of vertices.

1

0

2 3

Page 26: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Strongly Connected Graph

A directed graph which has a path between every pair of vertices.

Page 27: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Weakly Connected Graph

A directed graph which is connected, but not not strongly connected.

Page 28: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

A DAG that is not Connected

Page 29: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Connected Components

Page 30: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Review of Terms

• Undirected vs. directed graphs• Multiple edges, loops• Simple graph• Path• Cycle• Directed, acyclic graph (DAG)• Connected, strongly connected, and weakly

connected graphs• Connected component

Page 31: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Graph Implementations

Page 32: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Adjacency Matrix

01

3

2

0 1 2 3 0123

Page 33: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Adjacency Matrix

01

3

2 10 1 2 3

0123

Page 34: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Adjacency Matrix

01

3

2 1 0 0 1 2 3

0123

Page 35: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Adjacency Matrix

01

3

2 1 0 1 0 1 2 3

0123

Page 36: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Adjacency Matrix

01

3

2 1 0 1 10 1 2 3

0123

Page 37: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Adjacency Matrix

01

3

2 1 0 1 1

0 0 0 1

0 1 2 3 0123

Page 38: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Adjacency Matrix

01

3

2 1 0 1 1

0 0 0 1

1 0 0 0

0 1 2 3 0123

Page 39: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Adjacency Matrix

01

3

2 1 0 1 1

0 0 0 1

1 0 0 0

0 1 0 0

0 1 2 3 0123

Page 40: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

C++ Adjacency Matrix

01

3

2 1 0 1 1

0 0 0 1

1 0 0 0

0 1 0 0

0 1 2 3 0123

bool graph_adjacency[4][4];

Page 41: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Adjacency Matrix: Weighted Edges

01

3

2 5.8 0 6.0 1.5

0 0 0 2.5

2.5 0 0 0

0 4.2 0 0

0 1 2 3

0123

double graph_adjacency[4][4];

6.0

2.5

4.2

2.5

5.8

1.5

Page 42: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Weighted Edges: An Example

$320$245

San FransiscoBoston

Chicago

$452

$60 $180Dallas

Flights. What’s the cheapest way to fly San Fransisco Boston?

Page 43: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Weighted Edges: An Example

18001200

San FransiscoBoston

Chicago

2800

2300 1600Dallas

Mileage for various hops from San Fransisco Boston*

*Mileage is not real, but for sake of example.

Page 44: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Weighted Edges: An Example

Fran Pat

ADSL provider100Kbs

Available internet bandwidth on various paths between an ADSL provider and customers*.

*This is a simplication for sake of example, and bit rates are fictional.

1Gbs 100Kbs

500Kbs

Page 45: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Adjacency Matrix: Undirected Graph

01

3

2

0 1 2 3 0123

bool graph_adjacency[4][4];

Page 46: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Adjacency Matrix: Undirected Graph

01

3

2 0

0

0

0

0 1 2 3 0123

No loops zero diagonal.

Page 47: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Adjacency Matrix: Undirected Graph

01

3

2 0 1 1 1

0

0

0

0 1 2 3 0123

Page 48: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Adjacency Matrix: Undirected Graph

01

3

2 0 1 1 1

1 0 0 1

0

0

0 1 2 3 0123

Page 49: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Adjacency Matrix: Undirected Graph

01

3

2 0 1 1 1

1 0 0 1

1 0 0 0

0

0 1 2 3 0123

Page 50: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Adjacency Matrix: Undirected Graph

01

3

2 0 1 1 1

1 0 0 1

1 0 0 0

1 1 0 0

0 1 2 3 0123

Page 51: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Symmetry of Adjacency Matrix of Undirected Graph

01

3

2 0 1 1 1

1 0 0 1

1 0 0 0

1 1 0 0

0 1 2 3 0123

a[i][j]== a[j][i]

Page 52: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Non-symmetric Adjacency Matrix for Directed Graph

01

3

2 1 0 1 1

0 0 0 1

1 0 0 0

0 1 0 0

0 1 2 3 0123

a[i][j] a[j][i]

Page 53: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Symmetric Adjacency Matrix

• An undirected graph will always have a symmetric adjacency matrix.

• A directed graph may not have a symmetric adjacency matrix.

• If a directed graph has a symmetric matrix, what does that mean?

Page 54: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Non-symmetric Adjacency Matrix Unweighted Directed Graph

01

3

2 1 0 1 1

0 0 0 1

1 0 0 0

0 1 0 0

0 1 2 3 0123

a[i][j] a[j][i]

Page 55: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Add an Edge

01

3

2 1 0 1 1

0 0 0 1

1 0 0 0

1 1 0 0

0 1 2 3 0123

a[i][j]=a[j][i]

Page 56: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Adjacency Matrix for Weighted Directed Graph

01

3

2 5 0 3 6

0 0 0 4

3 0 0 0

6 4 0 0

0 1 2 3 0123

a[i][j]=a[j][i]

3

32

6

6

4

45

Page 57: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Time Complexity Analysis

Given N we have vertices in a graph, and we use the adjacency matrix representation.

1. What is the worst case and average complexity of inserting an edge in the graph?

2. What is the worst case and average complexity of removing an edge in the graph?

3. What is the complexity of retrieving a list of all nodes that share and edge with a particular vertex?

Page 58: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Representing Graphs with Edge Lists

01

3

2

2

3Null

Edge list for vertex 0

Null

Edge list for vertex 1

0

Edge list for vertex 2

1Null

Edge list for vertex 3

Null

Page 59: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Time Complexity Analysis

Given N we have vertices in a graph, and we use the edge list representation.

1. What is the worst case and average complexity of inserting an edge in the graph?

2. What is the worst case and average complexity of removing an edge in the graph?

3. What is the complexity of retrieving a list of all nodes that share and edge with a particular vertex?

Page 60: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Representing Graphs with Edge Sets

01

3

2

Assume a Set template class is available:

Declare array of sets, one per node in graph:

Set<int> connections[N];

Each node’s set contains the indices of all vertices that it is connected to.

Page 61: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Time Complexity Analysis

Given N we have vertices in a graph, and we use the edge set representation.

1. What is the worst case and average complexity of inserting an edge in the graph?

2. What is the worst case and average complexity of removing an edge in the graph?

3. What is the complexity of retrieving a list of all nodes that share and edge with a particular vertex?

Page 62: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Graph Implementations Summary

• Adjacency matrix

• Edge lists

• Edge sets

Page 63: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Graph Searching

Page 64: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Graph Search

• Choice of container– If a stack is used as the container for adjacent

vertices, we get depth first search.– If a list is used as the container adjacent

vertices, we get breadth first search.

Page 65: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Breadth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a queue QFor each vertex u V – {s}

color[u] whitecolor[s] grayQ {s}While Q {

u head[Q];for each v Adjacent[u]

if color[v] = white { color[v] gray Enqueue(Q,v)}

Dequeue(Q)color[u] black;

}

0

3

2

1

5

4

Page 66: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Breadth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a queue QFor each vertex u V – {s}

color[u] whitecolor[s] grayQ {s}While Q {

u head[Q];for each v Adjacent[u]

if color[v] = white { color[v] gray Enqueue(Q,v)}

Dequeue(Q)color[u] black;

}

0

3

2

1

5

4s

Q =

Page 67: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Breadth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a queue QFor each vertex u V – {s}

color[u] whitecolor[s] grayQ {s}While Q {

u head[Q];for each v Adjacent[u]

if color[v] = white { color[v] gray Enqueue(Q,v)}

Dequeue(Q)color[u] black;

}

0

3

2

1

5

4s

Q =

Page 68: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Breadth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a queue QFor each vertex u V – {s}

color[u] whitecolor[s] grayQ {s}While Q {

u head[Q];for each v Adjacent[u]

if color[v] = white { color[v] gray Enqueue(Q,v)}

Dequeue(Q)color[u] black;

}

0

3

2

1

5

4s

Q = 0

Page 69: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Breadth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a queue QFor each vertex u V – {s}

color[u] whitecolor[s] grayQ {s}While Q {

u head[Q];for each v Adjacent[u]

if color[v] = white { color[v] gray Enqueue(Q,v)}

Dequeue(Q)color[u] black;

}

0

3

2

1

5

4

u

Q = 0

Page 70: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Breadth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a queue QFor each vertex u V – {s}

color[u] whitecolor[s] grayQ {s}While Q {

u head[Q];for each v Adjacent[u]

if color[v] = white { color[v] gray Enqueue(Q,v)}

Dequeue(Q)color[u] black;

}

0

3

2

1

5

4

u

Q = 0 1

Page 71: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Breadth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a queue QFor each vertex u V – {s}

color[u] whitecolor[s] grayQ {s}While Q {

u head[Q];for each v Adjacent[u]

if color[v] = white { color[v] gray Enqueue(Q,v)}

Dequeue(Q)color[u] black;

}

0

3

2

1

5

4

u

Q = 0 1 2

Page 72: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Breadth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a queue QFor each vertex u V – {s}

color[u] whitecolor[s] grayQ {s}While Q {

u head[Q];for each v Adjacent[u]

if color[v] = white { color[v] gray Enqueue(Q,v)}

Dequeue(Q)color[u] black;

}

0

3

2

1

5

4

u

Q = 0 1 2 3

Page 73: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Breadth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a queue QFor each vertex u V – {s}

color[u] whitecolor[s] grayQ {s}While Q {

u head[Q];for each v Adjacent[u]

if color[v] = white { color[v] gray Enqueue(Q,v)}

Dequeue(Q)color[u] black;

}

0

3

2

1

5

4

u

Q = 01 2 3

Page 74: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Breadth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a queue QFor each vertex u V – {s}

color[u] whitecolor[s] grayQ {s}While Q {

u head[Q];for each v Adjacent[u]

if color[v] = white { color[v] gray Enqueue(Q,v)}

Dequeue(Q)color[u] black;

}

0

3

2

1

5

4

u

Q = 01 2 3

Page 75: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Breadth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a queue QFor each vertex u V – {s}

color[u] whitecolor[s] grayQ {s}While Q {

u head[Q];for each v Adjacent[u]

if color[v] = white { color[v] gray Enqueue(Q,v)}

Dequeue(Q)color[u] black;

}

0

3

2

1

5

4

u

Q = 01 2 3 4

Page 76: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Breadth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a queue QFor each vertex u V – {s}

color[u] whitecolor[s] grayQ {s}While Q {

u head[Q];for each v Adjacent[u]

if color[v] = white { color[v] gray Enqueue(Q,v)}

Dequeue(Q)color[u] black;

}

0

3

2

1

5

4

u

Q = 012 3 4

Page 77: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Breadth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a queue QFor each vertex u V – {s}

color[u] whitecolor[s] grayQ {s}While Q {

u head[Q];for each v Adjacent[u]

if color[v] = white { color[v] gray Enqueue(Q,v)}

Dequeue(Q)color[u] black;

}

0

3

2

1

5

4u

Q = 012 3 4

Page 78: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Breadth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a queue QFor each vertex u V – {s}

color[u] whitecolor[s] grayQ {s}While Q {

u head[Q];for each v Adjacent[u]

if color[v] = white { color[v] gray Enqueue(Q,v)}

Dequeue(Q)color[u] black;

}

0

3

2

1

5

4u

Q = 012 3 4

Page 79: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Breadth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a queue QFor each vertex u V – {s}

color[u] whitecolor[s] grayQ {s}While Q {

u head[Q];for each v Adjacent[u]

if color[v] = white { color[v] gray Enqueue(Q,v)}

Dequeue(Q)color[u] black;

}

0

3

2

1

5

4u

Q = 0123 4

Page 80: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Breadth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a queue QFor each vertex u V – {s}

color[u] whitecolor[s] grayQ {s}While Q {

u head[Q];for each v Adjacent[u]

if color[v] = white { color[v] gray Enqueue(Q,v)}

Dequeue(Q)color[u] black;

}

0

3

2

1

5

4

u

Q = 0123 4

Page 81: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Breadth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a queue QFor each vertex u V – {s}

color[u] whitecolor[s] grayQ {s}While Q {

u head[Q];for each v Adjacent[u]

if color[v] = white { color[v] gray Enqueue(Q,v)}

Dequeue(Q)color[u] black;

}

0

3

2

1

5

4

u

Q = 0123 4 5

Page 82: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Breadth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a queue QFor each vertex u V – {s}

color[u] whitecolor[s] grayQ {s}While Q {

u head[Q];for each v Adjacent[u]

if color[v] = white { color[v] gray Enqueue(Q,v)}

Dequeue(Q)color[u] black;

}

0

3

2

1

5

4

u

Q = 01234 5

Page 83: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Breadth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a queue QFor each vertex u V – {s}

color[u] whitecolor[s] grayQ {s}While Q {

u head[Q];for each v Adjacent[u]

if color[v] = white { color[v] gray Enqueue(Q,v)}

Dequeue(Q)color[u] black;

}

0

3

2

1

5

4

u

Q = 01234 5

Page 84: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Breadth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a queue QFor each vertex u V – {s}

color[u] whitecolor[s] grayQ {s}While Q {

u head[Q];for each v Adjacent[u]

if color[v] = white { color[v] gray Enqueue(Q,v)}

Dequeue(Q)color[u] black;

}

0

3

2

1

5

4

u

Q = 012345

Page 85: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Breadth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a queue QFor each vertex u V – {s}

color[u] whitecolor[s] grayQ {s}While Q {

u head[Q];for each v Adjacent[u]

if color[v] = white { color[v] gray Enqueue(Q,v)}

Dequeue(Q)color[u] black;

}

0

3

2

1

5

4

uQ = 012345

Page 86: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Breadth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a queue QFor each vertex u V – {s}

color[u] whitecolor[s] grayQ {s}While Q {

u head[Q];for each v Adjacent[u]

if color[v] = white { color[v] gray Enqueue(Q,v)}

Dequeue(Q)color[u] black;

}

0

3

2

1

5

4

uQ =

Page 87: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Graph Search

• Choice of container– If a stack is used as the container for adjacent

vertices, we get depth first search.– If a list is used as the container adjacent

vertices, we get breadth first search.

Page 88: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Depth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a stack SFor each vertex u V – {s}

color[u] whitecolor[s] grayS {s}While S {

u = Pop(S)for each v Adjacent[u]

if color[v] = white { color[v] gray Push(S,v)}

color[u] black;}

0

3

2

1

5

4s

S =

Page 89: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Depth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a stack SFor each vertex u V – {s}

color[u] whitecolor[s] grayS {s}While S {

u = Pop(S)for each v Adjacent[u]

if color[v] = white { color[v] gray Push(S,v)}

color[u] black;}

0

3

2

1

5

4s

S =

Page 90: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Depth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a stack SFor each vertex u V – {s}

color[u] whitecolor[s] grayS {s}While S {

u = Pop(S)for each v Adjacent[u]

if color[v] = white { color[v] gray Push(S,v)}

color[u] black;}

0

3

2

1

5

4s

S = 0

Page 91: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Depth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a stack SFor each vertex u V – {s}

color[u] whitecolor[s] grayS {s}While S {

u = Pop(S)for each v Adjacent[u]

if color[v] = white { color[v] gray Push(S,v)}

color[u] black;}

0

3

2

1

5

4

u

S =

Page 92: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Depth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a stack SFor each vertex u V – {s}

color[u] whitecolor[s] grayS {s}While S {

u = Pop(S)for each v Adjacent[u]

if color[v] = white { color[v] gray Push(S,v)}

color[u] black;}

0

3

2

1

5

4

u

S = 1

Page 93: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Depth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a stack SFor each vertex u V – {s}

color[u] whitecolor[s] grayS {s}While S {

u = Pop(S)for each v Adjacent[u]

if color[v] = white { color[v] gray Push(S,v)}

color[u] black;}

0

3

2

1

5

4

u

S = 32 1

Page 94: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Depth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a stack SFor each vertex u V – {s}

color[u] whitecolor[s] grayS {s}While S {

u = Pop(S)for each v Adjacent[u]

if color[v] = white { color[v] gray Push(S,v)}

color[u] black;}

0

3

2

1

5

4

u

S = 3 2 1

Page 95: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Depth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a stack SFor each vertex u V – {s}

color[u] whitecolor[s] grayS {s}While S {

u = Pop(S)for each v Adjacent[u]

if color[v] = white { color[v] gray Push(S,v)}

color[u] black;}

0

3

2

1

5

4

u

S = 3 2 1

Page 96: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Depth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a stack SFor each vertex u V – {s}

color[u] whitecolor[s] grayS {s}While S {

u = Pop(S)for each v Adjacent[u]

if color[v] = white { color[v] gray Push(S,v)}

color[u] black;}

0

3

2

1

5

4

u

S = 32 1

Page 97: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Depth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a stack SFor each vertex u V – {s}

color[u] whitecolor[s] grayS {s}While S {

u = Pop(S)for each v Adjacent[u]

if color[v] = white { color[v] gray Push(S,v)}

color[u] black;}

0

3

2

1

5

4

u

S = 32 15

Page 98: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Depth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a stack SFor each vertex u V – {s}

color[u] whitecolor[s] grayS {s}While S {

u = Pop(S)for each v Adjacent[u]

if color[v] = white { color[v] gray Push(S,v)}

color[u] black;}

0

3

2

1

5

4

u

S = 32 15

Page 99: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

3

Depth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a stack SFor each vertex u V – {s}

color[u] whitecolor[s] grayS {s}While S {

u = Pop(S)for each v Adjacent[u]

if color[v] = white { color[v] gray Push(S,v)}

color[u] black;}

0

3

2

1

5

4

u

S = 52 1

Page 100: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

3

Depth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a stack SFor each vertex u V – {s}

color[u] whitecolor[s] grayS {s}While S {

u = Pop(S)for each v Adjacent[u]

if color[v] = white { color[v] gray Push(S,v)}

color[u] black;}

0

3

2

1

5

4

u

S = 52 14

Page 101: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

3

Depth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a stack SFor each vertex u V – {s}

color[u] whitecolor[s] grayS {s}While S {

u = Pop(S)for each v Adjacent[u]

if color[v] = white { color[v] gray Push(S,v)}

color[u] black;}

0

3

2

1

5

4

u

S = 52 14

Page 102: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Depth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a stack SFor each vertex u V – {s}

color[u] whitecolor[s] grayS {s}While S {

u = Pop(S)for each v Adjacent[u]

if color[v] = white { color[v] gray Push(S,v)}

color[u] black;}

0

3

2

1

5

4

u

S = 4352 1

Page 103: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Depth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a stack SFor each vertex u V – {s}

color[u] whitecolor[s] grayS {s}While S {

u = Pop(S)for each v Adjacent[u]

if color[v] = white { color[v] gray Push(S,v)}

color[u] black;}

0

3

2

1

5

4

u

S = 4352 1

Page 104: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Depth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a stack SFor each vertex u V – {s}

color[u] whitecolor[s] grayS {s}While S {

u = Pop(S)for each v Adjacent[u]

if color[v] = white { color[v] gray Push(S,v)}

color[u] black;}

0

3

2

1

5

4

u

S = 43521

Page 105: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Depth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a stack SFor each vertex u V – {s}

color[u] whitecolor[s] grayS {s}While S {

u = Pop(S)for each v Adjacent[u]

if color[v] = white { color[v] gray Push(S,v)}

color[u] black;}

0

3

2

1

5

4

u

S = 43521

Page 106: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Depth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a stack SFor each vertex u V – {s}

color[u] whitecolor[s] grayS {s}While S {

u = Pop(S)for each v Adjacent[u]

if color[v] = white { color[v] gray Push(S,v)}

color[u] black;}

0

3

2

1

5

4

u

S =

Page 107: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Depth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a stack SFor each vertex u V – {s}

color[u] whitecolor[s] grayS {s}While S {

u = Pop(S)for each v Adjacent[u]

if color[v] = white { color[v] gray Push(S,v)}

color[u] black;}

0

3

2

1

5

4

u

S =

Page 108: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Graph Search

• Choice of container– If a stack is used as the container for adjacent

vertices, we get depth first search.– If a list is used as the container adjacent

vertices, we get breadth first search.

Page 109: Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Depth First AlgorithmGiven graph G=(V,E) and source vertex s VCreate a stack SFor each vertex u V – {s}

color[u] whitecolor[s] grayS {s}While S {

u = Pop(S)for each v Adjacent[u]

if color[v] = white { color[v] gray Push(S,v)}

color[u] black;}

0

3

2

1

5

4

u

S =