






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 / 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
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
(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.
(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.
(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.
(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 B ∞ 0 C ∞ 0 D ∞ 0 E 0 0 F ∞ 0 G ∞ 0
(scratch paper)