CS 225 Data Structures and Software Principles Exam - Spring, 2004 - Prof. John Carl Earls, Exams of Data Structures and Algorithms

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

2010/2011

Uploaded on 06/14/2011

koofers-user-m07-1
koofers-user-m07-1 🇺🇸

10 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
University of Illinois at Urbana-Champaign
Department of Computer Science
Third Examination
CS 225 Data Structures and Software Principles
Sample Exam 1
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.
Name: SOLUTIONS
NetID:
Lab Section (Day/Time):
This is a closed book and closed notes exam. In addition, you are not allowed to use any
electronic aides of any kind.
You should have 6 sheets total (the cover sheet, plus numbered pages 1-10). The last sheet is
scratch paper; you may detach it while taking the exam, but must turn it in with the exam
when you leave.
Unless otherwise stated in a problem, assume the best possible design of a particular imple-
mentation is being used.
Unless the problem specifically says otherwise, (1) assume the code compiles, and thus any
compiler error is an exam typo (though hopefully there are not any typos), and (2) assume
you are NOT allowed to write any helper methods to help solve the problem, nor are you
allowed to use additional arrays, lists, or other collection data structures unless we have said
you can.
Problem Points Score Grader
1 20
2 20
3 20
4 30
Total 90
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download CS 225 Data Structures and Software Principles Exam - Spring, 2004 - Prof. John Carl Earls and more Exams Data Structures and Algorithms in PDF only on Docsity!

University of Illinois at Urbana-Champaign

Department of Computer Science

Third Examination

CS 225 Data Structures and Software Principles

Sample Exam 1

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.

Name: SOLUTIONS

NetID:

Lab Section (Day/Time):

  • This is a closed book and closed notes exam. In addition, you are not allowed to use any electronic aides of any kind.
  • You should have 6 sheets total (the cover sheet, plus numbered pages 1-10). The last sheet is scratch paper; you may detach it while taking the exam, but must turn it in with the exam when you leave.
  • Unless otherwise stated in a problem, assume the best possible design of a particular imple- mentation is being used.
  • Unless the problem specifically says otherwise, (1) assume the code compiles, and thus any compiler error is an exam typo (though hopefully there are not any typos), and (2) assume you are NOT allowed to write any helper methods to help solve the problem, nor are you allowed to use additional arrays, lists, or other collection data structures unless we have said you can.

Problem Points Score Grader

Total 90

  1. [Ghost of Euler – 20 points].

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; }

  1. [Vertices Nearby – 20 points].

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)

  1. [Algorithms – 30 points (6 points each)].

(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).

A B C D E F G

A 0 12 0 0 0 0 18

B 0 0 0 0 5 0 1

C 0 17 0 0 4 0 12

D 0 0 3 0 0 13 0

E 9 0 0 5 0 10 0

F 3 8 0 0 0 0 0

G 2 0 0 0 0 0 0

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

  • Start Step 1 Step 2 Step 3 Step 4 Step 5 Step 6 Step 7

(scratch paper)