Professional Documents
Culture Documents
1. Suppose that you have two different algorithms for solving a problem. To solve a problem of
size n, the first algorithm uses exactly n (log n) operations and the second algorithm uses
exactly n3 /2 operations. As n grows, which algorithm uses fewer operations?
(6 marks)
Consider the ratio between the number of comparisons made by the second algorithm and the
number of comparisons made by the first algorithm.
Since the log function monotonically increases function and functionality, for all n> 4.
As the value of n increases, the first algorithm would also have fewer comparisons than the second.
How can the final exams at a university be scheduled so that no student has two exams at the
same time?
This scheduling problem can be solved using a graph color model, with vertices representing
courses and with an edge between two vertices if there is a common student in the courses they
represent. Each time slot for a final exam is represented by a different color. A scheduling of the
exams corresponds to a coloring of the associated graph. For instance, suppose there are seven
finals to be scheduled. Suppose the courses are numbered 1 through 7.
(6 Marks)
2. Weighted graphs
The weighted graphs in the figures here show some major roads in New Jersey.
Part (a) shows the distances between cities on these roads;
part (b) shows the tolls.
a) Find a shortest route in distance between Newark and Camden, and between Newark and Cape
May, using these roads.
(6 marks)
b) Find a least expensive route in the terms of total tolls using the roads in the graph between the
pairs of cities in part (a) of this exercise.
(6 marks)
We have paths between Newark and Camden as and corresponding tolls as:
Between Newark and Camden
3. Tree:
Implement Kruskal’s algorithm to the below and calculate the weight and total cost.
• Start with a forest consisting of |V | trees, each one consisting of one vertex
• Consider edges E in increasing order of their weight; add an edge if it connects two trees, ie if
it does not create cycles.
(6 marks)
Kruskal's algorithm:
Consider a connected and undirected graph, a spanning tree is a subgraph which connects all the
vertices. A tree can have multiple different spanning tree.
There is a graph given and two subgraphs in the above figure. For the graph, the two subgraphs
span the tree. They link all the vertices together.
MST(Minimum spanning tree) is the spanning tree that has the lower than or equal to weight in all
the spanning tree for a weighted, connected and undirected graph. A tree could have several MST's,
but all MST's would have the same total weight.
Steps of an algorithm:
2) Choose the edge that is smallest. Check to see if the spanning tree forms a cycle. If the edge
forms a cycle then drop it otherwise include.
Step 1:
1 h g
2 g f
2 i c
4 a b
4 c f
6 i g
7 h i
7 c d
8 b c
8 a h
9 d e
10 f e
11 b h
14 d f
Step 2:
So the final MST would be: