







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
A sample exam for the cs 225 data structures and software principles course at the university of illinois at urbana-champaign. The exam consists of five problems covering various topics such as kruskal's algorithm, dijkstra's algorithm, prim's algorithm, graph traversal algorithms, and graph theory. Students are required to answer short questions, justify answers, and run algorithms to determine distances and spanning trees.
Typology: Exams
1 / 13
This page cannot be seen from the preview
Don't miss anything!








75 minutes permitted
Print your name, netID, and lab section day/time neatly in the space provided below; print your name at the upper right corner of every page.
(a) What two data structures are used to implement Kruskal’s Algorithm?
(b) Using big-O notation, indicate the running time of breadth-first search on a graph with V vertices and no edges.
(c) Draw a directed graph that does not have a legal topological sort.
(c) In Prim’s Algorithm, we said you choose an edge at each step, from among all edges that go from set S to set N. What do these two sets represent, and why would it be a problem to pick an edge that goes between two vertices in S?
(d) Under what conditions would running breadth-first search on an undirected graph, result in a breadth-first spanning forest of more than one tree, rather than a breadth-first spanning tree? That is, what kinds of undirected graphs would result in that kind of answer? Justify your answer.
(e) 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.
V dv kv dv kv dv kv dv kv dv kv dv kv dv kv dv kv A ∞ 0 B ∞ 0 C ∞ 0 D ∞ 0 E 0 0 F ∞ 0 G ∞ 0
(b) What is the order of growth of the running time of depth-first search on a graph with V vertices and E edges? Express your answer in big-O notation and justify your answer.
You are given the following class:
class TreeNode { public: int vertexNumber; list<TreeNode*> subtrees; // initially an empty list; the function // push_back(item) will add item to // end of list };
In addition, you are given an adjacency matrix implementation of an unweighted, undirected, connected graph – such a graph, when you run breadth-first search on it, would produce a single spanning tree, rather than a spanning forest, no matter which vertex you start at. In this adjacency matrix implementation, the graph has vertices labelled with indices 0 through n-1, and you are given the value n and a two-dimensional array with n rows and n columns, both indexed from 0 through n-1. (In C++, a two-dimensional array is accessed via expressions such as arr[i][j] where arr is of type int**.) You want to write a method that takes those two values – the array and the n – as parameters, and returns a pointer to the root of the breadth-first-spanning-tree of the graph. You are allowed to use as many Queues as you like, as well as being allowed to create other one-dimensional arrays as well.
TreeNode* BreadthFirstSpanningTree(int** graph, int n) { // your code goes here
Suppose you have a directed weighted graph of n vertices, where the vertex numbers are 1 through n, and the graph implementation is an adjacency list. The adjacency list is repre- sented with an Array, indexed from 1 to n, of pointers to the following type:
class EdgeNode { public: int index; //index of target vertex int weight; //weight of edge EdgeNode* next; // ptr to next edge };
We want to convert this graph to an undirected weighted graph, by adding for each existent edge from u to v, the edge in the opposite direction, that is from v to u, with the same weight. That is, if the directed version had:
5 U ---------------------------> V
the undirected vertion will have:
5 U ---------------------------> V 5 V ---------------------------> U
You can assume that, initially, if there is an edge <i, j> in the graph, there is not an edge <j, i> of any weight. Write the function ConvertToUndirected, which receives as a parameter an Array of EdgeNode pointers – i.e. our adjacency list, as described above – by reference. The function will perform the conversion discussed above. Do not assume any edge list is specifically sorted in any way.
void ConvertToUndirected(Array<EdgeNode*>& graph) { // your code goes here
(Converting to undirected, continued)
(scratch paper)