







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
Material Type: Exam; Professor: Earls; Class: Data Structures; Subject: Computer Science; University: University of Illinois - Urbana-Champaign; Term: Spring 2011;
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?
Heaps and Disjoint Sets
(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.
There are many. Here is the simplest one: A ----> B /|\ | | | |________| The presence of a cycle means that there is no topological sort. Please note that the absence of edges means there is a topological sort – in fact, in a graph with no edges, every possible ordering of the vertices is a topological sort, since no ordering places a target before its source.
(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?
Set S is the set of all vertices that have been added to the tree; set N is the set of all vertices not yet added to the tree. If we chose an edge between two vertices in S, that means we are connecting two vertices that are both already in the tree – that is, we are connecting two vertices that are both already connected to each other. This means we are connecting them a second way – which means we have created a cycle.
(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.
If the undirected graph is NOT connected, then there will not be a path from the start vertex, to every other vertex in the graph, and thus we will not be able to reach every other vertex once we begin searching from our start vertex. This means once we finish our first tree in the breadth-first search, there will still be unmarked vertices, meaning we won’t have a single breadth-first spanning tree, but rather, will have to start one or more additional trees to get those other vertices.
(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 5 0 5 0 5 1 5 1 5 1 5 1 5 1 B ∞ 0 ∞ 0 ∞ 0 13 0 11 0 11 1 11 1 11 1 C ∞ 0 12 0 12 0 8 0 8 1 8 1 8 1 8 1 D ∞ 0 2 0 2 1 2 1 2 1 2 1 2 1 2 1 E 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 F ∞ 0 ∞ 0 ∞ 0 ∞ 0 ∞ 0 27 0 27 0 27 1 G ∞ 0 ∞ 0 21 0 21 0 21 0 21 0 21 1 21 1
(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.
Each call to depth-first search:
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 SOLUTION ON NEXT PAGE
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 for (int i = 1; i <= graph.Size(); i++) { EdgeNode ptr = graph[i]; while (ptr != NULL) { EdgeNode* travSearch = graph[ptr->index]; while ((travSearch != NULL) && (travSearch->index != i)) travSearch = travSearch->next; if (travSearch == NULL) { EdgeNode* temp = new EdgeNode(); temp->index = i; temp->weight = ptr->weight; temp->next = graph[ptr->index]; graph[ptr->index] = temp; } ptr = ptr->next; } }
(Converting to undirected, continued)
(scratch paper)