






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 sample solutions for the third exam of the cs 225 data structures and software principles course offered at the university of illinois at urbana-champaign in spring, 2004. The exam covers topics such as graph representation, minimum spanning tree, topological sort, cycle detection, and dijkstra's algorithm.
Typology: Exams
1 / 11
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.
You have the following EdgeNode class:
class EdgeNode { public: int index; // index of target vertex EdgeNode* next; // ptr to next edge };
Furthermore, you have a variable of type Array<EdgeNode> that is indexed from 1 to the size of the array (given by the Size() method in the Array class). The array is an adjacency list implementation of a graph, of the kind we first discussed in lecture; the vertices have indices from 1 to Size(). You want to write a method numOddDegree which has one parameter, a reference to an Array<EdgeNode> as described above. You can assume this adjacency list represents an undirected graph. You want to return the number of vertices with odd degree. (The degree of a vertex in an undirected graph, is the number of edges which that vertex is an endpoint for. So, you want to know how many vertices are endpoint to an odd number of edges.) You can assume the graph has no self-loops (i.e. no vertex has an edge to itself.)
int numOddDegree(Array<EdgeNode>& graph) { // your code goes here int numOdd = 0; int currentVert = 1; while (currentVert <= graph.Size()) { EdgeNode trav = graph[currentVert]; int numEdges = 0; while (trav != NULL) { trav = trav->next; numEdges++; } if (numEdges % 2 == 1) numOdd++; currentVert++; } return numOdd; }
You are given an adjacency matrix implementation of an unweighted, directed graph – 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. You want to write a method that takes those two values (the integer n and an int** to the two-dimensional array) and returns a 1 if the distance from any vertex to any other vertex is always 2 or less, and returns a 0 if there is at least one vertex for which the minimum distance to some other vertex is 3 or more. You are allowed to create additional one-dimensional arrays if you need to.
int DistanceCheck(int** graph, int n) { // your code goes here int thereIsNotABadVertexYet = 1; int currVertex = 0; int* marks = new int[n]; while ((thereIsNotABadVertexYet) && (currVert < n)) { for (int i = 0; i < n; i++) marks[i] = 0; marks[currVert] = 1; for (int i = 0; i < n; i++) { if (graph[currVert][i] == 1) { marks[i] = 1; // mark everything one away from currVert for (int j = 0; j < n; j++) if (graph[i][j] == 1) marks[j] = 1; // mark everything one away from what is // one away from currVert } } int i = 0; while ((thereIsNotABadVertexYet) && (i < n)) if (marks[i] == 0) thereIsNotABadVertexYet = 0; else i++; } if (thereIsNotABadVertexYet == 1) currVert++; } delete[] marks; return (currVert == n); }
(Vertices Nearby, continued)
(a) Give an example of a undirected, weighted graph that has two edges of equal weight, for which there is still a unique minimum spanning tree.
There are many examples of such a graph. As a trivial example, any undirected, weighted graph that is really a tree, and has two equal-weight edges, has a unique minimum spanning tree – the entire graph itself is the only possible spanning tree, since the graph itself is a tree.
(b) In the depth-first-search-based implementation of topological sort, if we moved the “post- visit” code to instead be “pre-visit” code (i.e. if we took the code we run after we explore a vertex’s neighbors, and instead run it before we explore a vertex’s neighbors), the algorithm will no longer work. Explain why.
The algorithm relied on the idea that, since you were assigning numbers to vertices from highest number to lowest number, as long as you assigned a number to a vertex after you assigned to all its neighbors, you guaranteed that your vertex got a lower number than its neighbors. If you instead assign to the vertex before exploring all its neighbors, perhaps there is a neighbor that has not been reached yet, that will now be assigned a number after your vertex gets a number...and thus will get a lower number than its prerequisite vertex, which is not allowed.
(c) When we implement breadth-first-search, it is important that we mark a vertex as “en- countered” before it is enqueued, rather than after it is dequeued. Explain why it is important – i.e. explain why we couldn’t instead choose to mark a vertex “encountered” after dequeueing it.
When we dequeue a vertex, we will proceed to inspect its neighbors. If one of those neighbors is on the queue already, but not marked “encountered”, we would add it to the queue a second time. And therefore, we would ultimately dequeue that neighbor twice and process it twice. Marking vertices as “encountered” once we have first reached them prevents us from adding a vertex to the queue multiple times, since every time we reach that vertex after the first time we reach it, it will already have the “encountered” flag set and we will therefore ignore that vertex.
(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).
V dv kv dv kv dv kv dv kv dv kv dv kv dv kv dv kv A ∞ 0 9 0 9 0 9 0 9 1 9 1 9 1 9 1 B ∞ 0 ∞ 0 ∞ 0 25 0 21 0 18 0 18 1 18 1 C ∞ 0 ∞ 0 8 0 8 1 8 1 8 1 8 1 8 1 D ∞ 0 5 0 5 1 5 1 5 1 5 1 5 1 5 1 E 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 F ∞ 0 10 0 10 0 10 0 10 0 10 1 10 1 10 1 G ∞ 0 ∞ 0 ∞ 0 20 0 20 0 20 0 19 0 19 1
(scratch paper)