Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

122
Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes dified from authors’ slides

Transcript of Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Chapter 20:

Graphs

CS 302 - Data StructuresMehmet H Gunes

Modified from authors’ slides

What is a graph?

• A data structure that consists of a set of nodes (vertices) and a set of edges between the vertices.

• The set of edges describes relationships among the vertices.

1 2

3 4

Terminology

• Definition:– A set of points that are joined by lines

• Graphs also represent the relationships among data items

• G = { V , E }– a graph is a set of vertices and edges

• A subgraph consists of a subset of a graph’s vertices and a subset of its edges

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Formally

• a graph G is defined as follows:

G = (V,E)

• where– V(G) is a finite, nonempty set of vertices– E(G) is a set of edges

• written as pairs of vertices

An undirected graph

The order of vertices in E is not important forundirected graphs!!

A graph in which the edges have no direction

A directed graph

A graph in which each edge is directed from one vertex to another (or the same) vertex

The order of vertices in E is important fordirected graphs!!

A directed graph

Trees are special cases of graphs!

Terminology

• Undirected graphs: edges do not indicate a direction

• Directed graph, or digraph: each edge has a direction

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Terminology

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

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Terminology

• Graphs that are (a) connected; (b) disconnected; and (c) complete

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Terminology

• (a) A multigraph is not a simple graph; (b) a self edge is not allowed in a simple graph

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

• Path: A sequence of vertices that connects two nodes in a graph

• The length of a path is the number of edges in the path.

e.g., a path from 1 to 4

1 2

3 4

<1, 2, 3, 4>

Graph terminology

Terminology

• Simple path: passes through vertex only once• Cycle: a path that begins and ends at same

vertex• Simple cycle: cycle that does not pass through

other vertices more than once• Connected graph: each pair of distinct vertices

has a path between them

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Graph terminology

Complete graph: A graph in which every vertex is directly connected to every other vertex

Terminology

• Complete graph: each pair of distinct vertices has an edge between them

• Graph cannot have duplicate edges between vertices– Multigraph: does allow multiple edges

• When labels represent numeric values, graph is called a weighted graph

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

• What is the number of edges E in a complete undirected graph with V vertices?

E=V* (V-1) / 2

Graph terminology (cont.)

or O(V2)

• What is the number of edges E in a complete directed graph with V vertices?

E=V * (V-1)

Graph terminology (cont.)

or O(V2)

A weighted graph

Weighted graph: A graph in which each edge carries a value

18

Terminology

• (a) a weighted graph; (b) a directed graphData Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Graphs as ADTs

ADT graph operations– Test whether graph is empty.– Get number of vertices in a graph.– Get number of edges in a graph.– See whether edge exists between two given

vertices.– Insert vertex in graph whose vertices have distinct

values that differ from new vertex’s value.– Insert edge between two given vertices in graph.

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Graphs as ADTs

ADT graph operations, ctd.– Remove specified vertex from graph and any

edges between the vertex and other vertices.– Remove edge between two vertices in graph.– Retrieve from graph vertex that contains given

value.

•View interface for undirected, connected graphs, Listing 20-1

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Array-Based Implementation

• Use a 1D array to represent the vertices• Use a 2D array (i.e., adjacency matrix) to

represent the edges

• Adjacency Matrix: – for a graph with N nodes,

• an N by N table that shows the existence (and weights) of all edges in the graph

to node x ?

from node x ?

Adjacency Matrix for Flight Connections

Array-Based Implementation (cont.)

• Memory required– O(V+V2)=O(V2)

• Preferred when– The graph is dense: E = O(V2)

• Advantage– Can quickly determine if there is an edge between two vertices

• Disadvantage– Consumes significant memory for sparse large graphs

Linked Implementation

• Use a 1D array to represent the vertices • Use a list for each vertex v which contains the

vertices which are adjacent from v (i.e., adjacency list)

• Adjacency List: – A linked list that identifies all the vertices to which a

particular vertex is connected; • each vertex has its own adjacency list

Adjacency List Representation of Graphs

to node x ?

from node x ?

Link-List-based Implementation (cont.)

• Memory required– O(V + E)

• Preferred when– for sparse graphs: E = O(V)

• Disadvantage– No quick way to determine the

vertices adjacent to a given vertex

• Advantage– Can quickly determine the vertices adjacent from a given vertex

O(V) for sparse graphs since E=O(V)

O(V2) for dense graphs since E=O(V2)

Implementing Graphs

• (a) A directed graph and (b) its adjacency matrix

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Implementing Graphs

• (a) A weighted undirected graph and (b) its adjacency matrix

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Implementing Graphs

• (a) A directed graph and (b) its adjacency list

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Implementing Graphs

• (a) A weighted undirected graph and (b) its adjacency list

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Graph Traversals

• Visits all of the vertices that it can reach– Happens if graph is connected

• Connected component is subset of vertices visited during traversal that begins at given vertex

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Graph searching

• Problem: find if there is a path between two vertices of the graph – e.g., Austin and Washington

• Methods: Depth-First-Search (DFS) or Breadth-First-Search (BFS)

Depth-First-Search (DFS)

• Main idea:– Travel as far as you can down a path – Back up as little as possible when you reach a

"dead end“• i.e., next vertex has been "marked" or there is no next

vertex

startVertex endVertex

Depth First Search: Follow Down

DFS uses Stack !

2

1

3

startVertex endVertex

(initialization) 36

37

endVertex

38

Depth-First Search• Goes as far as possible from a vertex before backing

up• Recursive algorithm

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Depth-First Search• Iterative algorithm, using a stack

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Depth-First Search• Iterative algorithm, using a stack, ctd.

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Depth-First Search

• Visitation order for (a) a depth-first search; (b) a breadth-first search

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Depth-First Search

• A connected graph with cycles

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Depth-First Search

• The results of a depth-first traversal, beginning at vertex a, of the graph

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Breadth-First-Searching (BFS)

• Main idea:– Look at all possible paths at the same depth

before you go at a deeper level– Back up as far as possible when you reach a

"dead end“• i.e., next vertex has been "marked" or there is no next

vertex

startVertex endVertex

Breadth First: Follow Across

BFS uses Queue !

1

2

3

4

5

6

78

Breadth First Uses Queue

startVertex endVertex

(initialization)

48

49

50

Breadth-First Search

• Visits all vertices adjacent to vertex before going forward

• Breadth-first search uses a queue

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Breadth-First Search

• The results of a breadth-fi rst traversal, beginning at vertex a, of the graph

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Applications of Graphs

• A directed graph without cycles• Topological Sorting

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Applications of Graphs

• The graph arranged according to the topological orders (a) a, g, d, b, e, c, f and (b) a, b, g, d, e, f, c

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Applications of Graphs

• Topological sorting algorithm

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Applications of Graphs

• A trace of topSort1 for the graphData Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Applications of Graphs

• A trace of topSort1 for the graphData Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Applications of Graphs

• A trace of topSort1 for the graphData Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Applications of Graphs

• A trace of topSort1 for the graphData Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Applications of Graphs

• A trace of topSort2 for the graphData Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Spanning Trees

• Tree: an undirected connected graph without cycles

• Observations about undirected graphs1. Connected undirected graph with n vertices

must have at least n – 1 edges.2. Connected undirected graph with n vertices and

exactly n – 1 edges cannot contain a cycle3. A connected undirected graph with n vertices

and more than n – 1 edges must contain at least one cycle

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Spanning Trees

• The DFS spanning tree rooted at vertex a for the graph

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Spanning Trees

• DFS spanning tree algorithm

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Spanning Trees

• BFS spanning tree algorithm

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Spanning Trees

• The BFS spanning tree rooted at vertex a for the graph

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Minimum Spanning Trees

• A weighted, connected, undirected graph

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Minimum Spanning Trees

• Minimum spanning tree algorithm

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Minimum Spanning Trees

• A trace of primsAlgorithm for the graph, beginning at vertex a

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Minimum Spanning Trees

• A trace of primsAlgorithm for the graph, beginning at vertex a

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Minimum Spanning Trees

• A trace of primsAlgorithm for the graph, beginning at vertex a

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Graph Algorithms

• Depth-first search – Visit all the nodes in a branch to its deepest point

before moving up • Breadth-first search

– Visit all the nodes on one level before going to the next level

• Single-source shortest-path

– Determines the shortest path from a designated starting node to every other node in the graph

71

Single Source Shortest Path

72

Single Source Shortest Path

• What does “shortest” mean?

• What data structure should you use?

73

Shortest-path problem

• There might be multiple paths from a source vertex to a destination vertex

• Shortest path: the path whose total weight (i.e., sum of edge weights) is minimum

AustinHoustonAtlantaWashington: 1560 miles AustinDallasDenverAtlantaWashington:

2980 miles74

Variants of Shortest Path

• Single-pair shortest path– Find a shortest path from u to v

• for given vertices u and v

• Single-source shortest paths– G = (V, E) find a shortest path from a given source

vertex s to each vertex v V

75

Variants of Shortest Paths (cont’d)

• Single-destination shortest paths– Find a shortest path to a given destination vertex t

from each vertex v– Reversing the direction of each edge single-source

• All-pairs shortest paths– Find a shortest path from u to v for every pair of

vertices u and v

76

Shortest Paths

• Shortest path between two vertices in a weighted graph has smallest edge-weight sum

(a) A weighted directed graph and (b) its adjacency matrix

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

• Weight of path p = v0, v1, . . . , vk

• Shortest-path weight from s to v:

min w(p) : s v if there exists a path from s to

v

∞ otherwise

Notation

k

iii vvwpw

11 ),()(

78

0

3 9

5 11

3

6

5

7

6

s

t x

y z

22 1

4

3

p

δ(v) =

Negative Weights and Negative Cycles

• Negative-weight edges may form negative-weight cycles.

• If negative cycles are reachable from the source, the shortest path is not well defined.– i.e., keep going around the cycle, and get

w(s, v) = - for all v on the cycle

0

3

-4

2

8

-6

s

a b

e f

-33

56

4

7

c d g

79

Could shortest path solutions contain cycles?

• Negative-weight cycles– Shortest path is not well defined

• Positive-weight cycles:– By removing the cycle, we can get a shorter path

• Zero-weight cycles

– No reason to use them; can remove them to obtain a path with same weight

80

Shortest-path algorithms• Solving the shortest path problem in a brute-force

manner requires enumerating all possible paths.– There are O(V!) paths between a pair of vertices in an

acyclic graph containing V nodes.

• We will discuss two algorithms– Dijkstra’s algorithm– Bellman-Ford’s algorithm

81

Shortest-path algorithms

• Dijkstra’s and Bellman-Ford’s algorithms are “greedy” algorithms!– Find a “globally” optimal solution by making “locally”

optimum decisions.

• Dijkstra’s algorithm– Does not handle negative weights.

• Bellman-Ford’s algorithm– Handles negative weights but not negative cycles reachable

from the source.

82

Shortest-path algorithms (cont’d)

• Both Dijkstra’s and Bellman-Ford’s algorithms are iterative:

– Start with a shortest path estimate for every vertex: d[v]

– Estimates are updated iteratively until convergence:

d[v]δ(v)

83

Shortest-path algorithms (cont’d)

• Two common steps:

(1) Initialization

(2) Relaxation (i.e., update step)

84

0

6

5

7

7

9

s

t x

8-3

2-4

-2

Initialization Step• Set d[s]=0

– i.e., source vertex

• Set d[v]=∞ for– i.e., large value

v s

85

Relaxing an edge (u, v) implies testing whether we can improve the shortest path to v found so far by going through u:

If d[v] > d[u] + w(u, v)

we can improve the shortest path to v d[v]=d[u]+w(u,v)

Relaxation Step

5 92

u v

5 72

u v

RELAX(u, v, w)

5 62

u v

5 62

u v

RELAX(u, v, w)

s s

no change

86

Bellman-Ford Algorithm• Can handle negative weights.

• Detects negative cycles reachable from the source.

• Returns FALSE if negative-weight cycles are reachable from the source s no solution

87

Bellman-Ford Algorithm (cont’d)• Each edge is relaxed |V–1| times by making |V-1|

passes over the whole edge set– to make sure that each edge is relaxed exactly |V – 1| times

• it puts the edges in an unordered list and goes over the list |V – 1| times

0

6

5

7

7

9

s

t x

y z

8-3

2-4

-2

(t, x), (t, y), (t, z), (x, t), (y, x), (y, z), (z, x), (z, s), (s, t), (s, y)

88

Example

0

6

5

7

7

9

s

t x

y z

8-3

2-4

-2

0

6

5

7

7

9

s

t x

y z

8-3

2-4

-2

E: (t, x), (t, y), (t, z), (x, t), (y, x), (y, z), (z, x), (z, s), (s, t), (s, y)

6

7

Pass 1

89

Example

0

6

7

6

5

7

7

9

s

t x

y z

8-3

2-4

-2

(t, x), (t, y), (t, z), (x, t), (y, x), (y, z), (z, x), (z, s), (s, t), (s, y)

0

6

7

6

5

7

7

9

s

t x

y z

8-3

2-4

-211

2

4

0

6

7

6

5

7

7

9

s

t x

y z

8-3

2-4

-211

2

42

0

6

7

6

5

7

7

9

s

t x

y z

8-3

2-4

-211

2

42

-2

Pass 1(from previousslide)

Pass 2

Pass 3 Pass 4

90

Detecting Negative Cycles: needs an extra iteration

for each edge (u, v) E doif d[v] > d[u] + w(u, v) then return FALSE

return TRUE

0

c

s b2

3-8

0

c

s b2

3-8

2

5

-3 -3 2

5

c

s b2

3-8

-1

2

-6

Consider edge (s, b):

d[b] = -1d[s] + w(s, b) = -4

d[b] > d[s] + w(s, b) d[b]=-4 (d[b] keeps changing!)

1st pass 2nd pass

(s,b) (b,c) (c,s)

91

BELLMAN-FORD Algorithm1. INITIALIZE-SINGLE-SOURCE(V, s)2. for i ← 1 to |V| - 13. for each edge (u, v) E4. RELAX(u, v, w)5. for each edge (u, v) E6. if d[v] > d[u] + w(u, v)7. return FALSE8. return TRUE Time: O(V+VE+E)=O(VE)

O(V)

O(V)

O(E)

O(E)

O(VE)

92

Dijkstra’s Algorithm

• Cannot handle negative-weights!– w(u, v) > 0, (u, v) E

• Each edge is relaxed only once!

93

Dijkstra’s Algorithm (cont’d)

• At each iteration, it maintains two sets of vertices:

d[v]=δ (v) d[v]≥δ (v)

V

S V-S

estimates have converged to the shortest path solution

estimates have notconverged yet

Initially, S is empty94

Dijkstra’s Algorithm (cont.)

• Vertices in V–S reside in a min-priority queue Q

– Priority of u determined by d[u]

– The “highest” priority vertex will be the one having the

smallest d[u] value.

95

Dijkstra (G, w, s)

0

10

1

5

2

s

t x

y z

2 3

9

7

4 6 0

10

1

5

2

s

t x

y z

2 3

9

7

4 6

10

5

S=<> Q=<s,t,x,z,y> S=<s> Q=<y,t,x,z>

Initialization

96

Example (cont.)

0

10

5

10

1

5

2

s

t x

y z

2 3

9

7

4 6

8 14

7

0

8 14

5 7

10

1

5

2

s

t x

y z

2 3

9

7

4 6

13

S=<s,y> Q=<z,t,x> S=<s,y,z> Q=<t,x>

97

Example (cont.)

0

8 13

5 7

10

1

5

2

s

t x

y z

2 3

9

7

4 6

9

0

8 9

5 7

10

1

5

2

s

t x

y z

2 3

9

7

4 6

S=<s,y,z,t> Q=<x> S=<s,y,z,t,x> Q=<>

Note: use back-pointers to recover the shortest path solutions!

98

Dijkstra (G, w, s) INITIALIZE-SINGLE-SOURCE(V, s)

S ←

Q ← V[G]

while Q

u ← EXTRACT-MIN(Q)

S ← S {u}

for each vertex v Adj[u]

RELAX(u, v, w)

Update Q (DECREASE_KEY)

Overall: O(V+2VlogV+(Ev1+Ev2+...)logV)

=O(VlogV+ElogV)=O(ElogV)

build priority heap

O(V logV)

O(V) times

O(logV)

O(Evi)

O(logV)

O(V)

O(Evi logV)

99

Improving Dijkstra’s efficiency• Suppose the shortest path from s to w is the

following:

• If u is the i-th vertex in this path, it can be shown that d[u] δ (u) at the i-th iteration:– move u from V-S to S– d[u] never changes again

w

s xu

… …

100

Add a flag for efficiency!

INITIALIZE-SINGLE-SOURCE(V, s)

S ←

Q ← V[G]

while Q

u ← EXTRACT-MIN(Q)

S ← S {u};

for each vertex v Adj[u]

RELAX(u, v, w)

Update Q (DECREASE_KEY)

If v not marked

mark u

101

Dijkstra vs Bellman-Ford

• Bellman-Ford O(VE)

• Dijkstra

O(ElogV)

V2

V3

if G is sparse: E=O(V)

if G is dense: E=O(V2)

VlogV

V2logV

if G is sparse: E=O(V)

if G is dense: E=O(V2)102

Shortest Paths

• Dijkstra’s shortest-path algorithm

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Shortest Paths

• Dijkstra’s shortest-path algorithm, ctd.

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Shortest Paths

• A trace of the shortest-path algorithm applied to the graph

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Shortest Paths

• Checking weight [u] by examining the graph: (a) weight [2] in step 2; (b) weight [1] in step 3;

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Shortest Paths

• Checking weight [u] by examining the graph: (c) weight [3] in step 3; (d) weight [3] in step 4

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

• BFS can be used to solve the shortest path problem when the graph is weightlessweightless or when all the weights are equal.– Path with lowest number of edges

• i.e., connections

• Need to “mark” vertices before Enqueue!– i.e., do not allow duplicates

Revisiting BFS

108

Circuits

• Another name for a type of cycle common in statement of certain problems

• Circuits either visit every vertex once or visit every edge once

• An Euler circuit begins at a vertex v, passes through every edge exactly once, and terminates at v

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Circuits

• (a) Euler’s bridge problem and (b) its multigraph representation

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Circuits

• Pencil and paper drawings

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Circuits

• Connected undirected graphs based on the drawings

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Circuits

• The steps to determine an Euler circuit for the graph

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Circuits

• The steps to determine an Euler circuit for the graph

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Some Difficult Problems

• Hamilton circuit– Path that begins at a vertex v, passes through

every vertex in the graph exactly once, and terminates at v

• The traveling salesperson problem– Variation of Hamilton circuit– Involves a weighted graph that represents a road

map– Circuit traveled must be the least expensive

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Some Difficult Problems

• The three utilities problem

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Some Difficult Problems

• Planar graph– Can draw it in a plane in at least one way so that

no two edges cross

• The four-color problem– Given a planar graph, can you color the vertices so

that no adjacent vertices have the same color, if you use at most four colors?

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Some Difficult Problems

1. Describe the graphs in Figure 20-32 . For example, are they directed? Connected? Complete? Weighted?

2. Use the depth-first strategy and the breadth-first strategy to traverse the graph in Figure 20-32 a, beginning with vertex 0. List the vertices in the order in which each traversal visits them.

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Some Difficult Problems

3. Write the adjacency matrix for the graph in Figure 20-32 a.

4. Add an edge to the directed graph in Figure 20-14 that runs from vertex d to vertex b. Write all possible topological orders for the vertices in this new graph.

5. Is it possible for a connected undirected graph with fi ve vertices and four edges to contain a simple cycle? Explain.

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Some Difficult Problems

6. Draw the DFS spanning tree whose root is vertex 0 for the graph in Figure 20-33 .

7. Draw the minimum spanning tree whose root is vertex 0 for the graph in Figure 20-33 .

8. What are the shortest paths from vertex 0 to each vertex of the graph in Figure 20-24 a? (Note the weights of these paths in Figure 20-25 .)

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Some Difficult Problems

• FIGURE 20-32 Graphs for Checkpoint Questions 1, 2, and 3

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Some Difficult Problems

• FIGURE 20-33 A graph for Checkpoint Questions 6 and 7 and for Exercises 1 and 4

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013