You are on page 1of 9

Part 3 (CLO3): Complexity of algorithm

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)

As n increases, the first algorithm performs better than the second.

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.

Part 4 (CLO4): Graph

1. Scheduling Final Exams

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)

(a) Let's have different cities abbreviated as:


Newark , Woodbridge , Trento , Camden , Asbury park , Atlantic , Cape May

Between Newark and Camden

Tree vertices Neighboring vertices Shortest from n

The shortest path therefore is through the wood bridge .


Between Newark and Cape May

Tree vertices Neighboring vertices Shortest from n

Hence shortest path is via wood bridge and Camden

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

Tree vertices Neighboring vertices Cheapest from n

Hence least expensive path is via wood bridge.


We have zero tolls between Wood Bridge and Camden, Camden and Cape May. There least
expensive route is via Wood Bridge and Camden between Newark and Cape May.

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.

The given graph is :


The cost of a spanning tree is all the edges of the tree by the number of weights. A MST has edges
of (V-1) where the number of vertices in the given graph is V.

Steps of an algorithm:

1) In ascending order of their weight, sort all the edges.

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.

3) repeat step 2 until the spanning tree has V-1 edges.

Step 1:

Weight Src. Dest.

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:

The total cost(weight) = 4+8+7+9+2+4+1+2=37


The MST has (V-1) or (9-1) i.e. 8 edges.

You might also like