0% found this document useful (0 votes)
31 views3 pages

Ps 5

Uploaded by

sunshiyao0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views3 pages

Ps 5

Uploaded by

sunshiyao0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Problem Set 5

Design and Analysis of Algorithms, Fall 2024

Due: December 06 23:59:59


106
(UTC+8), mail to [email protected]. Algorithms
106
106 Algorithms
Algorithms
Exercises
Exercises
ProblemExercises
1 3.1. Perform a depth-first search on the following graph; whenever there’s a choice of vertices, pick
the one that is alphabetically first. Classify each edge as a tree edge or back edge, and give the
3.1.3.1.
Perform a depth-first
Perform a depth-first search on on
search thethe
following graph; whenever
following there’s a choice of vertices, pick
(a) Perform a the BFS traversal on the pre and post
following number graph;
graph. of eachwhenever
Whenever
there’s
vertex.there’s a choice of vertices, pick
oneone
the that is alphabetically
that is alphabeticallyfirst. Classify
first. each
Classify edge
each as as
edge a tree edge
a tree or a
edge orchoice
back edge,
back of vertices,
andand
edge, give thethepick the one
give
pre
that is alphabeticallyandand
pre post number
first.
post number
Show ofthe
each vertex.
of each vertex. value and the parent value for each vertex.
distance A B C
A A B B C C

D E F
D D E E F F

G H I
G G H H I I
3.2. Perform depth-first search on each of the following graphs; whenever there’s a choice of vertices,
pick the one that is alphabetically first. Classify each edge as a tree edge, forward edge, back
3.2.3.2.
Perform
Performdepth-first search
depth-first on on
search each of the
each following
of edge,
the graphs; whenever
following there’s a choice of vertices,
edge, or cross and givegraphs;
the prewhenever
and postthere’s
number a choice
of eachofvertex.
vertices,
(b) Perform a pickDFS thetraversal
pick oneone
the that is
onalphabetically
that isthe
alphabetically
following first. Classify
first. each
Classify
graph. edge
each
Whenever asthere’s
edge a tree
as edge,
a tree forward
aedge,
choice ofedge,
forward back
edge, backpick the one
vertices,
edge, or cross
edge, edge,
or cross andand
edge, give thethe
give pre andand
pre post number
post numberof each vertex.
of each vertex.
that is alphabetically first. Show the classification of edges, and give the start/finish A B
(a) (b) times of vertices.
A B C A A B B
(a) (a) (b) (b)
H C
A A B B C C
H H C C
E D
G D
E E D D
G G D D
F G H F E

F F G G H H F F E E
3.3. Run the DFS-based topological ordering algorithm on the following graph. Whenever you have
(c) Run the3.3.DFS-based topological asorting
choice of algorithm
vertices to explore,
ononthe always pick thegraph.
following one thatWhenever
is alphabetically first.a choice
there’s
Run
3.3. thethe
Run DFS-based
DFS-basedtopological ordering
topological algorithm
ordering algorithm thethe
on following graph.
following Whenever
graph. Whenever youyou
have
have
of vertices, picka choice
athe of vertices
choice
oneof that to explore,
vertices
is to always
explore, pick
always
alphabetically thethe
pick
first.one that
one
Show is
that
thealphabetically
is final
alphabeticallyfirst.
ordering.first.
A D G
A A D D C G G F
C C B F F E H
B B E E H H
(a) Indicate the pre and post numbers of the nodes.
(a) (a)
Indicate thethe
Indicate prepre (b)post
andand
post What are the
numbers
numbers sources
of the
of and sinks of the graph?
nodes.
the nodes.
(b) (b)
What areare
What thethe
sources (c) What
andand
sources sinks topological
of the
sinks graph?
of the ordering
graph? is found by the algorithm?
(c) (c)
What topological
What topological (d) How
ordering is many
is found
ordering by topological
found thethe
by orderings does this graph have?
algorithm?
algorithm?
Problem 2 (d)(d)HowHow
many topological
many orderings
topological
3.4. Run does
orderings this
does
the strongly graph
this have?
graph
connected have?
components algorithm on the following directed graphs G. When
doing DFS on GR : whenever there is a choice of vertices to explore, always pick the one that is
3.4.3.4.
Run the strongly connected
Run the strongly connected components
componentsalgorithm on the following directed graphs When
algorithm on the following directed graphs G. When
G.
alphabetically first.
(a) doing
Give an example DFS
doing ofonaon
DFS : whenever there
: whenever
Gdirected
R R
G graph is
Gais=
there choice
(V,ofE),
a choice vertices to explore,
of vertices
a source always
to explore,
vertex s pick
always Vthe
∈ pick oneone
, the
and that is is
a that
set of tree edges
alphabetically first.
alphabetically first.
Etree ⊆ E such that for each vertex v ∈ V , the unique simple path in the graph (V, E ) from s to v
tree
is a shortest path in G, yet the set of edges Etree cannot be produced by running BFS on G, no matter
how the vertices are ordered in each adjacency list.
(b) Give a counterexample to the conjecture that if a directed graph G contains a path from u to v, and
if u.d < v.d in a depth-first search of G, then v is a descendant of u in the depth-first forest produced.
(c) Prove or disprove: If a directed graph G contains cycles, then the DFS-based topological sorting
algorithm introduced in class produces a vertex ordering that minimizes the number of “bad” edges that
are inconsistent with the ordering produced.

1
33 14 21 32 33 14 21 32 33 14 21 32 33 14 21 32 33 14 21 32 33 14 21 32
23 31 12 23 23 31 12 23 23 31 12 23 23 31 12 23 23 31 12 23 23 31 12 23
2 3 1 2 2 3 1 2 2 3 1 2 2 3 1 2 2 3 1 2 2 3 1 2
Figure 5.16. A five-move solution for a 4 × 4 Vidrach Itky Leda puzzle.
Figure 5.16. A five-move solution for a 4 × 4 Vidrach Itky Leda puzzle.
Problem Group I [Pick any two of the following three problems, but you
14. Suppose you are given a directed graph G = (V, E) and two vertices s and t.
get
14. extra credit
Suppose
Describe and
if you
you analyze
are solve
given all of them.]
ana algorithm
directed graph G = (V, ifE)there
to determine and is
two in Gs from
vertices
a walk and t.
s
to t I.1
Describe and analyze
(possibly an algorithm
repeating to determine
vertices and/or edges)if whose
there islength
a walk is G from s
in divisible
Problem
t (possibly repeating vertices and/or edges) whose length is divisible
to 3.
by
by 3.
Suppose you are given a directed graph G = (V, E) and two vertices s and t. Describe and analyze
Fortoexample,
an algorithm determine ifgiven
there isthe graph
a walk shown
in G from s to below, with
t (possibly the indicated
repeating ver-
vertices and/or edges)
lengths isand
whosetices For t, your
example,
divisible given
by 3. For the graph
example, given
algorithm shown
the graph
should below, with
shownTrue,
return the
below, your indicated
algorithm
because ver-
theshould
walk return
True,tices s
because and
the t,
sw y  x sw t has length 6.
walk s →
your w →
algorithm
y → x →
should
s → w →
return t True,
has lengthbecause
6. To get the
full walk
credit, your
swshould
algorithm  y  run
x in
sO(|V
w t| +has
|E|)length
time. 6.
s w t
s w t

x y z
x y z

15. Suppose you are given a directed graph G where some edges are red and the
15. remaining
Suppose you are given
edges a directed
are blue. Describe graph an Galgorithm
where some edges
to find theare red andwalk
shortest the
Problem
in G I.2
remaining
from oneedges vertex s to another
are blue. Describevertex t in which
an algorithm to find the shortest
no three consecutivewalk
Supposein G
youfrom
edges have one
theavertex
are given directed
same sgraph
to another
color. G where
That is, vertex
some
if theedgest inare
walk which
red andno
contains thethree
two redconsecutive
remaining edgesin
edges area blue.
Describe
row, and
edges the analyze
have
nexttheansame
algorithm
edge must to find
color.
be That
blue,theand
shortest
is, walk
ififthe
the walkin G
walk from onetwo
contains
contains vertex
twored s toedges
blue another ina t
edgesinvertex
in which no three consecutive edges have the same color. That is, if the walk contains two red edges
arow,
row,thethenext
next edge
edge must
must bebe blue,
red.and if the walk contains two blue edges in
in a row, the next
a row, edge must
the next edgebemust
blue, be
andred.
if the walk contains two blue edges in a row, the next edge
ForFor
must be red. example,
example, given thefollowing
given the following graphgraph as input,
as input, your algorithm
your algorithm should return should
the integer
return the integer 7, because s→a→b⇒d→c⇒a→b→t is the shortest legal your
7, because For
s→a→bexample,
=⇒ given
d→c =⇒ the
a→b→tfollowing
is the graph
shortest as
legalinput,
walk your
from s algorithm
to t. To get should
full credit,
return
algorithm
walk should
from s toin t.
therun
integerO(|V7,| because
+ |E|) time.s→a→b⇒d→c⇒a→b→t is the shortest legal
walk from s to t. s a b
s a b

c d t
c d t

214
214 Problem I.3
Suppose the computer science curriculum at UCAS consists of n courses, and all of them are mandatory.
The prerequisite graph G = (V, E) has a node for each course, and an edge from course v to course
w if and only if v is a prerequisite for w. Devise an algorithm that works directly with this graph
representation, and computes the minimum number of semesters necessary to complete the curriculum
(assume that a student can take any number of courses in one semester). The running time of your
algorithm should be O(|V | + |E|).

Problem 3
Consider the two-pass strongly-connected components (SCC) algorithm we introduced in class. Pro-
fessor Bacon claims that the algorithm would still work correctly if it used the transpose graph in the
second depth-first search and scanned the vertices in the order of increasing finishing times. Do you
agree with Professor Bacon? You need to justify your answer.

2
Problem 4
Given a directed graph G = (V, E), devise an algorithm to create another graph G′ = (V, E ′ ) such that:
(a) G′ has the same strongly connected components as G, (b) G′ has the same component graph as G,
and (c) E ′ is as small as possible. Your algorithm needs to return G′ and have runtime O(|V | + |E|).
You also need to give the pseudocode of your algorithm.

Problem 5
You are given a directed graph G = (V, E) in which each node u ∈ V has an associated price pu which
is a positive integer. Define array cost as follows: for each u ∈ V , cost[u] is the price of the cheapest
node reachable from u (including u itself). Your goal is to design an algorithm that fills in the entire
cost array (i.e., for all vertices).
(a) Give an O(|V | + |E|) time algorithm that works for directed acyclic graphs.
(b) Give an O(|V | + |E|) time algorithm that works for all directed graphs.

Problem 6
A directed graph G = (V, E) is “sort-of-connected” if, for every pair of vertices u and v, either u is
reachable from v or v is reachable from u (or both).
(a) Give an example of a directed acyclic graph with a unique source that is not sort-of-connected.
(b) Devise an algorithm with O(|V | + |E|) runtime to determine whether a given directed acyclic graph
is sort-of-connected.
(c) Devise an algorithm with O(|V | + |E|) runtime to determine whether a given directed graph is
sort-of-connected.

You might also like