Homework 5 - DFS
The homework is due at the start of class on Thursday, April 30.
Late homeworks will not be accepted.
The homework must be in english.
Practice problems (don't turn in):
-
Problem 3.3 from [DPV] (Implementing topological sorting algorithm)
-
Problem 3.4 from [DPV] (Implementing SCC algorithm)
-
Min-Heap (or priority queue):
Starting from an empty min-Heap, perform the following sequence of operations, and
draw the final Min-Heap data structure.
-
Insert a;7 (i.e., insert element a with key 7).
-
Insert b;4.
-
Insert c;9.
-
Insert d;12.
-
Insert e;10.
-
Insert f;3.
-
Decrease-Key of e to 2.
-
Delete-Min
Homework problems (turn in):
-
Problem 3.8 from [DPV] Pouring water
-
Problem 3.15 from [DPV] Computopia (2 points)
-
Running (2 points):
To get in shape, you have decided to start running to work.
You want a route that goes entirely uphill and then entirely downhill,
so that you can work up a sweat going uphill and then get a nice breeze
at the end of your run as you run faster downhill.
Your run will start at home and end at work and you are given as input
a map detailing the roads with n intersections and m one-way road segments,
each road segment is marked uphill or downhill.
You can assume home is
intersection 1 and work is intersection n,
and the input is given by two sets:
EU representing the
uphill edges and ED representing the downhill edges,
where each set is given in adjacency list representation.
Give an algorithm that determines if there is a route (regardless of the length)
which starts at home, only goes uphill, and then only
goes downhill, and finishes at work.
You should give a clear description of your algorithm in words,
and you should explain its running time.
Faster (in O() notation) and correct is worth more credit.
Here is an example with 5 vertices, where vertex 1 is home and vertex 5 is work.
The input are the following arrays of linked lists:
EU = 1→2; 3→4 ; 3→5.
ED = 1→3; 2→3; 2→4; 4→5.
There is one uphill-downhill way to go from 1 to 5: 1→2→4→5.