CS 225 Third Exam - Fall 2005: Data Structures and Software Principles, Study notes of Data Structures and Algorithms

The third examination for the cs 225 data structures and software principles course offered at the university of illinois at urbana-champaign during the fall 2005 semester. The exam covers various topics such as minheaps, disjoint sets, dijkstra's algorithm, graph balancing, and graph connectivity. Students are required to complete multiple programming problems related to these topics.

Typology: Study notes

Pre 2010

Uploaded on 03/16/2009

koofers-user-h9v
koofers-user-h9v 🇺🇸

10 documents

1 / 12

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
Fall 2005
7:00pm 8:15pm Monday, November 14
Name:
NetID:
Lab Section (Day/Time):
This is a closed book and closed notes exam. No electronic aids are allowed, either.
You should have 6 sheets total (the cover sheet, plus numbered pages 1-11). 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. The second-to-last page contains some class and function declarations you
have seen before; you can use this sheet as reference while taking the exam.
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 15
2 15
3 15
4 15
5 30
Total 90
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download CS 225 Third Exam - Fall 2005: Data Structures and Software Principles and more Study notes 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

Fall 2005

7:00pm – 8:15pm Monday, November 14

Name:

NetID:

Lab Section (Day/Time):

  • This is a closed book and closed notes exam. No electronic aids are allowed, either.
  • You should have 6 sheets total (the cover sheet, plus numbered pages 1-11). 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. The second-to-last page contains some class and function declarations you have seen before; you can use this sheet as reference while taking the exam.
  • 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. [Special Sets – 15 points].

(a) Complete the method below, which reassigns the priority of the value at cell index in the minheap, to be updatedPriority. We will assume this means the priority is decreased. Your method, in addition to performing the update, should repair the partial ordering of the heap. void DecreasePriority(Array& minheap, int index, int updatedPriority) { // your code goes here

while ((index > 1) && (updatedPriority < minHeap[index/2])) { minHeap[index] = minHeap[index/2]; index = index/2; }

minHeap[index] = updatedPriority; }

Note: there were two different interpretations of this question: decreasing the literal priority value (thus making the value more important in a min-heap), and decreasing the actual importance of the value (by increasing the literal priority value). In exam rooms where this was not officially clarified, we accepted both interpretations.

  1. [Dijkstra’s Algorithm – 15 points].

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.

A B C D E F G A 0 0 0 0 2 3 20

B 7 0 5 0 0 12 0

C 0 0 0 1 0 6 10

D 0 0 0 0 19 0 17

E 0 0 0 0 0 0 3

F 0 0 0 0 0 0 1

G 10 0 0 0 0 10 0

V dv kv dv kv dv kv dv kv dv kv dv kv dv kv dv kv A ∞ 0 7 0 7 0 7 0 7 1 7 1 7 1 7 1 B 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 C ∞ 0 5 0 5 1 5 1 5 1 5 1 5 1 5 1 D ∞ 0 ∞ 0 6 0 6 1 6 1 6 1 6 1 6 1 E ∞ 0 ∞ 0 ∞ 0 25 0 9 0 9 1 9 1 9 1 F ∞ 0 12 0 11 0 11 0 10 0 10 0 10 1 10 1 G ∞ 0 ∞ 0 15 0 15 0 15 0 12 0 11 0 11 1

  • Start Step 1 Step 2 Step 3 Step 4 Step 5 Step 6 Step 7
  1. [Adjacency List Coding – 15 points].

You are given the EdgeNode class seen on page N of this exam. You have a variable graph of type Array<EdgeNode*>, which implements an adjacency list of a graph, as we have discussed in class. The indices of the array run from 1 through graph.size(), and you can assume this is a directed graph. Your task is to find the balance of each vertex in the graph. We will define the balance of a vertex, to be the number of departing edges of the vertex, minus the number of entering edges. (So, a vertex with the same number of entering and departing edges, would have a balance of zero.) When you have completed that task, your method should return the index of the vertex with the maximum balance. (If two or more vertices tie for the maximum balance, you can return any of those indices.) You are allowed to use one variable of type Array in this problem.

int getBalance(Array<EdgeNode*> graph) { // your code goes here

Array balances;

for (int i = 1; i <= graph.size(); i++) balances[i] = 0;

for (int i = 1; i <= graph.size(); i++) { EdgeNode* trav = graph[i]; while (trav != NULL) { balances[i]++; balances[trav->target]--; trav = trav->next; } }

int maxBalance = 1; for (int i = 2; i <= graph.size(); i++) if (balances[i] > balances[maxBalance]) maxBalance = i; return i; }

  1. [Analysis of Graph Algorithms – 30 points].

(a) For a graph of V vertices and E edges, what is the minimum number of edges that might be inspected by Kruskal’s algorithm? Justify your answer. (5 points)

Even if you approve every edge you inspect, then you have to still inspect the V-1 edges that ultimately get added to the tree. So, V-1 is the answer. (You add V-1 edges to the tree because every tree has one fewer edges than vertices, and you have V vertices.)

(b) Give an example of a directed, acyclic graph with four vertices, for which there is exactly one legal topological sort order for the vertices of the graph. (5 points)

a->b->c->d

(c) Recall that the complement of a graph G, is a graph that contains the same vertices as the original graph G, and that contains an edge between two vertices x and y if and only if that edge does NOT exist in the original graph G. Consider the problem of finding the complement of a directed graph G containing V vertices and E edges, when that graph G is implemented with an adjacency list in which the edge list of each vertex is sorted from lowest target index to highest target index. (The complement you create should be implemented in a similar manner.) What is the order of growth of the worst-case running time of finding the complement of such a graph? Given your answer using big-O notation, and explain your answer in sufficient detail to assure us you know what you are talking about. (Note: think carefully about what original graph would result in the longest time possible to create the complement of that graph.) (10 points)

The running time is O(V 2 ). That is actually the order of growth of the best, average, and worst cases. Traverse down the edge list of a vertex, for every vertex in your original graph. This is normally O(V + E), because you spend constant time at every node in every edge list of the graph (moving one more edge forward in the list, and inspecting the target of the edge to which you just moved). However, you also spend constant time on every edge NOT in the graph...since you have to add those edges to the complement. You can add an edge to the complement in constant time (keep a tail pointer to your new edge list as you go, so that you can quickly append to the end of the list). So, for every edge list, you perform constant work on S edges and constant work on V-S edges, meaning that processing one vertex involves constant time on each of the V possible edges. And then you do that for every vertex in the array. Thus, O(V 2 ) for edge processing, and thus O(V 2 + V ) = O(V 2 ) time overall.

This is info regarding the Array class and EdgeNode class used on this exam

class EdgeNode { public: int target; EdgeNode* next; };

template class Array // Here are the member function declarations for the Array class; // we’ve left the declarations for the iterators and iterator // support functions (begin(), end(), etc.) out; you don’t need them. Array(); // size 0 array, indiced 0 through - Array(int low, int high); // indices low through high Array(Array const & origVal); // copy constructor ~Array(); // destructor Array const & operator=(Array const & origVal);//assignment op Etype const & operator[](int index) const; // accesses cell at param index

Etype & operator[](int index); // accesses cell at param index void initialize(Etype const & initElement); // inits all cells to param void setBounds(int theLow, int theHigh); // changes bounds of array, int size() const; // returns number of cells in array int lower() const; // returns lowest index int upper() const; // returns upper index

(scratch paper, page 1)