• Home
  • Textbooks
  • Data Structures and Algorithms in C++
  • GRAPHS

Data Structures and Algorithms in C++

Adam Drozdek

Chapter 8

GRAPHS - all with Video Answers

Educators


Chapter Questions

Problem 1

Look carefully at the definition of a graph. In one respect, graphs are more specific than trees. What is it?

Check back soon!
03:04

Problem 2

What is the relationship between the sum of the degrees of all vertices and the number of edges of graph $G=(V, E)$ ?

Mengchun Cai
Mengchun Cai
Numerade Educator
01:37

Problem 3

What is the complexity of breadthFirstSearch()?

James Kiss
James Kiss
Numerade Educator
03:56

Problem 4

Show that a simple graph is connected if it has a spanning tree.

Victoria Dollar
Victoria Dollar
Numerade Educator
04:04

Problem 5

Show that a tree with $n$ vertices has $n-1$ edges.

WZ
Wen Zheng
Numerade Educator

Problem 6

How can DijkstraAlgorithm( ) be applied to undirected graphs?

Check back soon!
01:57

Problem 7

How can DijkstraAlgorithm ( ) be modified to become an algorithm for finding the shortest path from vertex $a$ to $b$ ?

WZ
Wen Zheng
Numerade Educator

Problem 8

The last clause from genericshortestPathAlgorithm()
add $\mathrm{u}$ to toBechecked if it not there;
is not included in DijkstraAlgorithm ( ). Can this omission cause any trouble?

Check back soon!

Problem 9

Modify FordAlgorithm ( ) so that it does not fall into an infinite loop if applied to a graph with negative cycles.

Check back soon!

Problem 10

For what digraph does the while loop of FordAlgorithm() iterate only one time? Two times?

Check back soon!

Problem 11

Can FordAlgorithm ( ) be applied to undirected graphs?

Check back soon!

Problem 12

Make necessary changes in FordAlgorithm ( ) to adapt it to solving the all-to-one shortest path problem and apply the new algorithm to vertex $f$ in the graph in Figure 8.8. Using the same order of edges, produce a table similar to the table shown in this figure.

Check back soon!

Problem 13

The D'Esopo-Pape algorithm is exponential in the worst case. Consider the following method to construct pathological graphs of $n$ vertices (Kershenbaum 1981), each vertex identified by a number $1, \ldots, n$ :
KershenbaumAlgorithm( )
construct a two-vertex graph with vertices 1 and 2 , and edge $(1,2)=1$;
for $(k=3 ; k<=n ; k++)$
add vertex $\mathrm{k}$;
for $(i=2 ; i<k ; i++)$
add edge $(k, i)$ with weight $(\operatorname{edge}(k, i))=\operatorname{weight}(\operatorname{edge}(1, i))$;
weight $($ edge $(1, \mathrm{i}))=$ weight $(1, \mathrm{i})+2^{\mathrm{k}-3}+1 ;$
add edge $(1, \mathrm{k})$ with weight $(\operatorname{edge}(1, \mathrm{k}))=1$;
The vertices adjacent to vertex 1 are put in ascending order and the remaining adjacency lists are in descending order. Using this algorithm, construct a five-vertex graph and execute the D'Esopo-Pape algorithm showing all changes in the deque and all edge updates. What generalization can you make about applying Pape's method to such graphs?

Check back soon!

Problem 14

What do you need to change in genericshortestPathAlgorithm() in order to convert it to Dijkstra's one-to-all algorithm?

Check back soon!
02:47

Problem 15

Enhance WFIalgorithm ( ) to indicate the shortest paths, in addition to their lengths.

Khoobchandra Agrawal
Khoobchandra Agrawal
Numerade Educator

Problem 16

WFIalgorithm ( ) finishes execution gracefully even in the presence of a negative cycle. How do we know that the graph contains such a cycle?

Check back soon!

Problem 17

The original implementation of WFIalgorithm ( ) given by Floyd is as follows:
WFIalgorithm2 (matrix weight)
for $i=1$ to $|V|$
for $j=1$ to $|V|$
if weight $[j, i]<\infty$
for $k=1$ to $|V|$
if weight $[i, k]<\infty$
if (weight $[j][k]>\operatorname{weight}[j][i]+\operatorname{weight}[i][k])$
weight $[j][k]=\operatorname{weight}[j][i]+\operatorname{weight}[i][k]$;
Is there any advantage to this longer implementation?

Check back soon!
00:36

Problem 18

One method of finding shortest paths from all vertices to all other vertices requires us to transform the graph so that it does not include negative weights. We may be tempted to do it by simply finding the smallest negative weight $k$ and adding $-k$ to the weights of all edges. Why is this method inapplicable?

WZ
Wen Zheng
Numerade Educator
03:07

Problem 19

For which edges does $\leq$ in the inequality
$$
\operatorname{dist}(v) \leq \operatorname{dist}(w)+w e i g h t(\text { edge }(w v)) \text { for any vertex } w
$$
become <?

Prathan Jarupoonphol
Prathan Jarupoonphol
Numerade Educator

Problem 20

Modify cycleDetectionDFS ( ) so that it could determine whether a particular edge is part of a cycle in an undirected graph.

Check back soon!
01:21

Problem 21

When would KruskalAlgorithm () require $|E|$ iterations?

Vysakh M
Vysakh M
Numerade Educator
00:25

Problem 22

Our implementation of union ( ) requires three arrays. Is it possible to use only two of them and still have the same information concerning roots, next vertices, and lengths? Consider using negative numbers.

Prathan Jarupoonphol
Prathan Jarupoonphol
Numerade Educator
08:13

Problem 23

How can the second minimum spanning tree be found?

Chris Trentman
Chris Trentman
Numerade Educator
07:42

Problem 24

Is the minimum spanning tree unique?

Chris Trentman
Chris Trentman
Numerade Educator
05:12

Problem 25

How can the algorithms for finding the minimum spanning tree be used to find the maximum spanning tree?

Chris Trentman
Chris Trentman
Numerade Educator

Problem 26

The algorithm blocksearch ( ), when used for undirected graphs, relies on the following observation: In a depth-first search tree created for an undirected graph, each back edge connects a successor to a predecessor (and not, for instance, a sibling to a sibling). Show the validity of this observation.

Check back soon!
01:07

Problem 27

What is the complexity of blocksearch () ?

Narayan Hari
Narayan Hari
Numerade Educator

Problem 28

Blocks in undirected graphs are defined in terms of edges, and the algorithm blockDFS ( ) stores edges on the stack to output blocks. On the other hand, SCCs in digraphs are defined in terms of vertices, and the algorithm strongDFS ( ) stores vertices on the stack to output SCC. Why?

Check back soon!

Problem 29

Consider a possible implementation of topologicalsort () by using in it the following routine:
minimalvertex (digraph)
$\mathrm{v}=$ a vertex of digraph;
while $\mathrm{v}$ has a successor
$\mathrm{v}=$ successor $(\mathrm{v}) ;$
return $\mathrm{v} ;$
What is the disadvantage of using this implementation?

Check back soon!

Problem 30

A tournament is a digraph in which there is exactly one edge between every two vertices.
a. How many edges does a tournament have?
b. How many different tournaments of $n$ edges can be created?
c. Can each tournament be topologically sorted?
d. How many minimal vertices can a tournament have?
e. A transitive tournament is a tournament which has edge $(v w)$ if it has edge $(v u)$ and edge $(u w)$. Can such a tournament have a cycle?

Check back soon!
06:28

Problem 31

Does considering loops and parallel edges complicate the analysis of networks? How about multiple sources and sinks?

Khoobchandra Agrawal
Khoobchandra Agrawal
Numerade Educator
00:23

Problem 32

FordFulkersonAlgorithm () assumes that it terminates. Do you think such an assumption is safe?

Donald Albin
Donald Albin
Numerade Educator

Problem 33

FordFulkersonAlgorithm ( ) executed in a depth-first fashion has some redundancy. First, all outgoing edges are pushed onto the stack and then the last is popped off to be followed by the algorithm. For example, in the network in Figure 8.20a, first, all three edges coming out of vertex $s$ are pushed, and only afterward is the last of them, edge(se), followed. Modify FordFulkersonAlgorithm () so that the first edge coming out of a certain vertex is immediately followed, and the second is followed only if the first does not lead to the sink. Consider using recursion.

Check back soon!
06:03

Problem 34

Find the capacity of the cut determined by the set $X=\{s, d\}$ in the graph in Figure 8.19.

Rebekkah Bodanske
Rebekkah Bodanske
Numerade Educator
01:26

Problem 35

What is the complexity of DinicAlgorithm ( ) in a network where all edges have a capacity of one?

Nick Johnson
Nick Johnson
Numerade Educator
01:15

Problem 36

Why does DinicAlgor ithm () start from the sink to determine a layered network?

Ajay Singhal
Ajay Singhal
Numerade Educator

Problem 37

The member function readCommittees ( ) in the case study uses two trees, commit tee Tree and member Tree, to generate adjacency lists and then initialize the array vertices. However, one tree would be sufficient. What do you think is the reason for using two trees, not one?

Check back soon!

Problem 38

What would be the output of the program in the case study if linked lists in Figure $8.38 \mathrm{~b}$ were in the reverse order?

Check back soon!