






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The third examination for the cs 225 data structures and software principles course offered at the university of illinois at urbana-champaign during the fall 2005 semester. The exam covers various topics such as minheaps, disjoint sets, dijkstra's algorithm, graph balancing, and graph connectivity. Students are required to complete multiple programming problems related to these topics.
Typology: Study notes
1 / 12
This page cannot be seen from the preview
Don't miss anything!







7:00pm – 8:15pm Monday, November 14
(a) Complete the method below, which reassigns the priority of the value at cell index in the minheap, to be updatedPriority. We will assume this means the priority is decreased. Your method, in addition to performing the update, should repair the partial ordering of the heap. void DecreasePriority(Array
while ((index > 1) && (updatedPriority < minHeap[index/2])) { minHeap[index] = minHeap[index/2]; index = index/2; }
minHeap[index] = updatedPriority; }
Note: there were two different interpretations of this question: decreasing the literal priority value (thus making the value more important in a min-heap), and decreasing the actual importance of the value (by increasing the literal priority value). In exam rooms where this was not officially clarified, we accepted both interpretations.
For the given graph, run Dijkstra’s algorithm, indicating in the table below the distances at each vertex at the end of each step (dv), and whether or not the vertex has been marked known yet at the end of each step (kv). The initialization has already been done for you.
A B C D E F G A 0 0 0 0 2 3 20
B 7 0 5 0 0 12 0
C 0 0 0 1 0 6 10
D 0 0 0 0 19 0 17
E 0 0 0 0 0 0 3
F 0 0 0 0 0 0 1
G 10 0 0 0 0 10 0
V dv kv dv kv dv kv dv kv dv kv dv kv dv kv dv kv A ∞ 0 7 0 7 0 7 0 7 1 7 1 7 1 7 1 B 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 C ∞ 0 5 0 5 1 5 1 5 1 5 1 5 1 5 1 D ∞ 0 ∞ 0 6 0 6 1 6 1 6 1 6 1 6 1 E ∞ 0 ∞ 0 ∞ 0 25 0 9 0 9 1 9 1 9 1 F ∞ 0 12 0 11 0 11 0 10 0 10 0 10 1 10 1 G ∞ 0 ∞ 0 15 0 15 0 15 0 12 0 11 0 11 1
You are given the EdgeNode class seen on page N of this exam. You have a variable graph of type Array<EdgeNode*>, which implements an adjacency list of a graph, as we have discussed in class. The indices of the array run from 1 through graph.size(), and you can assume this is a directed graph. Your task is to find the balance of each vertex in the graph. We will define the balance of a vertex, to be the number of departing edges of the vertex, minus the number of entering edges. (So, a vertex with the same number of entering and departing edges, would have a balance of zero.) When you have completed that task, your method should return the index of the vertex with the maximum balance. (If two or more vertices tie for the maximum balance, you can return any of those indices.) You are allowed to use one variable of type Array
int getBalance(Array<EdgeNode*> graph) { // your code goes here
Array
for (int i = 1; i <= graph.size(); i++) balances[i] = 0;
for (int i = 1; i <= graph.size(); i++) { EdgeNode* trav = graph[i]; while (trav != NULL) { balances[i]++; balances[trav->target]--; trav = trav->next; } }
int maxBalance = 1; for (int i = 2; i <= graph.size(); i++) if (balances[i] > balances[maxBalance]) maxBalance = i; return i; }
(a) For a graph of V vertices and E edges, what is the minimum number of edges that might be inspected by Kruskal’s algorithm? Justify your answer. (5 points)
Even if you approve every edge you inspect, then you have to still inspect the V-1 edges that ultimately get added to the tree. So, V-1 is the answer. (You add V-1 edges to the tree because every tree has one fewer edges than vertices, and you have V vertices.)
(b) Give an example of a directed, acyclic graph with four vertices, for which there is exactly one legal topological sort order for the vertices of the graph. (5 points)
a->b->c->d
(c) Recall that the complement of a graph G, is a graph that contains the same vertices as the original graph G, and that contains an edge between two vertices x and y if and only if that edge does NOT exist in the original graph G. Consider the problem of finding the complement of a directed graph G containing V vertices and E edges, when that graph G is implemented with an adjacency list in which the edge list of each vertex is sorted from lowest target index to highest target index. (The complement you create should be implemented in a similar manner.) What is the order of growth of the worst-case running time of finding the complement of such a graph? Given your answer using big-O notation, and explain your answer in sufficient detail to assure us you know what you are talking about. (Note: think carefully about what original graph would result in the longest time possible to create the complement of that graph.) (10 points)
The running time is O(V 2 ). That is actually the order of growth of the best, average, and worst cases. Traverse down the edge list of a vertex, for every vertex in your original graph. This is normally O(V + E), because you spend constant time at every node in every edge list of the graph (moving one more edge forward in the list, and inspecting the target of the edge to which you just moved). However, you also spend constant time on every edge NOT in the graph...since you have to add those edges to the complement. You can add an edge to the complement in constant time (keep a tail pointer to your new edge list as you go, so that you can quickly append to the end of the list). So, for every edge list, you perform constant work on S edges and constant work on V-S edges, meaning that processing one vertex involves constant time on each of the V possible edges. And then you do that for every vertex in the array. Thus, O(V 2 ) for edge processing, and thus O(V 2 + V ) = O(V 2 ) time overall.
This is info regarding the Array class and EdgeNode class used on this exam
class EdgeNode { public: int target; EdgeNode* next; };
template
Etype & operator[](int index); // accesses cell at param index void initialize(Etype const & initElement); // inits all cells to param void setBounds(int theLow, int theHigh); // changes bounds of array, int size() const; // returns number of cells in array int lower() const; // returns lowest index int upper() const; // returns upper index
(scratch paper, page 1)