CS502 Solved Reference Subjective File by Faisal Dar (MIT)

33
CS502 - Fundamentals of Algorithms [email protected] CS502 Solved Reference Subjective File by Faisal Dar (MIT) 1. Give Detail Example of 2-d maxima Problem. (Pg17) Answer: Let a point p in 2-dimensional space be given by its integer coordinates, p = (p.x, p.y). A point p is said to dominated by point q if p.x q.x and p.y q.y. Given a set of n points, P = {p1, p2. . . pn} in 2-space a point is said to be maximal if it is not dominated by any other point in P. The problem is to output all the maximal points of P. We introduced a brute-force algorithm that ran in Θ (n 2 ) time. 2. Where Arise Clique Cover? (pg176) Answer: The clique cover problem arises in applications of clustering. We put an edge between two nodes if they are similar enough to be clustered in the same group. We want to know whether it is possible to cluster all the vertices into k groups. 3. Explain directed and undirected graphs. A graph is a mathematical structure that is made up of set of vertices and edges. A graph represents a set of objects (represented by vertices) that are connected through some links (represented by edges). Using mathematical notations, a graph can be represented by G, where G= (V, E) and V is the set of vertices and E is the set of edges. In an undirected graph there is no direction associated with the edges that connect the vertices. In a directed graph there is a direction associated with the edges that connect the vertices. Explain the Floyd-Warshall Algorithm (Running time and Space used) (pg161) Answer: It can be explained by these steps: Let G = (V, E) be a directed graph with edge weights. If (u, v) E is an edge then w (u, v) denotes its weight. δ (u, v) is the distance of the minimum cost path between u and v. We will allow G to have negative edges weights but will not allow G to have negative cost cycles. We will present a Θ (n 3 ) algorithm for the all pairs shortest path. The algorithm is called the Floyd-Warshall algorithm and is based on dynamic programming. Clearly, the running time is Θ (n 3 ). The space used by the algorithm is Θ (n 2 ). 4. What is heap and heap order? 2marks Answer: A heap is a left-complete binary tree that conforms to the heap order. The heap order property: in a (min) heap, the parent node has key smaller than or equal to both of its children nodes. Similarly, in a max heap, the parents have a key larger than or equal both of its children.

Transcript of CS502 Solved Reference Subjective File by Faisal Dar (MIT)

Page 1: CS502 Solved Reference Subjective File by Faisal Dar (MIT)

CS502 - Fundamentals of Algorithms [email protected]

CS502 Solved Reference Subjective File by Faisal Dar (MIT)

1. Give Detail Example of 2-d maxima Problem. (Pg17)

Answer:

Let a point p in 2-dimensional space be given by its integer coordinates, p = (p.x, p.y). A point p

is said to dominated by point q if p.x ≤ q.x and p.y ≤ q.y. Given a set of n points, P = {p1, p2. . .

pn} in 2-space a point is said to be maximal if it is not dominated by any other point in P. The

problem is to output all the maximal points of P. We introduced a brute-force algorithm that ran

in Θ (n2) time.

2. Where Arise Clique Cover? (pg176)

Answer:

The clique cover problem arises in applications of clustering. We put an edge between two nodes

if they are similar enough to be clustered in the same group. We want to know whether it is

possible to cluster all the vertices into k groups.

3. Explain directed and undirected graphs.

A graph is a mathematical structure that is made up of set of vertices and edges. A graph

represents a set of objects (represented by vertices) that are connected through some links

(represented by edges). Using mathematical notations, a graph can be represented by G, where

G= (V, E) and V is the set of vertices and E is the set of edges.

In an undirected graph there is no direction associated with the edges that connect the vertices.

In a directed graph there is a direction associated with the edges that connect the vertices.

Explain the Floyd-Warshall Algorithm (Running time and Space used) (pg161)

Answer:

It can be explained by these steps:

• Let G = (V, E) be a directed graph with edge weights.

• If (u, v) ∈ E is an edge then w (u, v) denotes its weight.

• δ (u, v) is the distance of the minimum cost path between u and v.

• We will allow G to have negative edges weights but will not allow G to have negative cost

cycles.

• We will present a Θ (n3) algorithm for the all pairs shortest path.

• The algorithm is called the Floyd-Warshall algorithm and is based on dynamic

programming.

• Clearly, the running time is Θ (n3).

• The space used by the algorithm is Θ (n2).

4. What is heap and heap order? 2marks

Answer:

• A heap is a left-complete binary tree that conforms to the heap order.

• The heap order property: in a (min) heap, the parent node has key smaller than or equal

to both of its children nodes.

• Similarly, in a max heap, the parents have a key larger than or equal both of its children.

Page 2: CS502 Solved Reference Subjective File by Faisal Dar (MIT)

CS502 - Fundamentals of Algorithms [email protected]

5. How do we covert the shortest distance problem in to a single source

problem? 2marks

Answer:

We can solve the shortest distance algorithm by using Dijkstra's algorithm in

Edge-weighted digraphs with nonnegative weights

Using extra space proportional to V and

Time proportional to E logs V (in the worst case).

6. How short path information propagate in graph using bell ford algorithm?

2marks

Answer:

The shortest path information is propagated sequentially along each shortest path in the graph.

Bellman-Ford allows negative weights edges and no negative cost cycles.

7. An arbitrary graph with G (V, E) with E = |V|- I is a tree. True or false tell

briefly. 2marks

Answer:

This statement is False. |V| is total number of vertices of a graph and |V|- I are the vertices

which are never revisited, if for every pair of vertices u, v ∈ Vc ( Vc means V-1) then graph

induced by Vc is a complete sub graph. Hint: in clique algorithm

8. Express arithmetic series in summation notation and tell big theta case.

3marks pg15

Answer:

Big Theta Case

9. How Dijkstra’s algorithm work 3marks

Answer:

10. Two Cases of Floyed Warshall algorithm 3marks

Answer:

Don’t go through k at all

The length of the shortest is d (k−1) ij

Page 3: CS502 Solved Reference Subjective File by Faisal Dar (MIT)

CS502 - Fundamentals of Algorithms [email protected]

Do go through k

The length of the path is d (k−1) ik+ d (k−1) kJ

11. Prove that Topological sort is directed acyclic graph 5marks Pg: 133

Answer:

In this figure we run DFS on DAG where we shows the linear order () obtained by the

topological sort of the sequence of putting on a suit. The DAG is still the same. As a result, all

directed edges go from left to right.

12. Calculate time complexity of 5marks

1 2 2

3 3 3 ……. ………. n n n n ……. n (n times)

Answer: http://answers.yahoo.com/question/index?qid=1006011700253

Page 4: CS502 Solved Reference Subjective File by Faisal Dar (MIT)

CS502 - Fundamentals of Algorithms

[email protected]

155

13. Write Pseudo code to relax a vertex for Dijkstra’s algorithm.

14. What is Heap sort Algorithm?

Answer:

A heap is a left-complete binary tree that conforms to the heap order.

Page 5: CS502 Solved Reference Subjective File by Faisal Dar (MIT)

CS502 - Fundamentals of Algorithms [email protected]

15. Difference between Back edge and cross edge

Answer:

16. Prim's Algorithm

Answer:

17. Explain Do go through k & don’t go through k at all?

Answer:

Don’t go through k at all

The length of the shortest is d (k−1) ij

Do go through k

The length of the path is d (k−1) ik+ d (k−1) kJ

18. Explain heapify?

Answer:

19. Prove that the generic TRAVERSE (S) marks every vertex in any connected

graph exactly once and the set of edges (v, parent (v)) with parent (v) ¹F

form a spanning tree of the graph. Pg125

Answer:

20. Apply Kruskal’s algorithms on the following graph. [You can

show final result in exam software and need not to show all intermediate

steps].

Page 6: CS502 Solved Reference Subjective File by Faisal Dar (MIT)

CS502 - Fundamentals of Algorithms

[email protected]

Answer:

Page no 147 to 149

21. Polynomial time algorithm… 169pg

Answer:

22.What are a run time analysis and its two criteria? pg13

Answer:

Page 7: CS502 Solved Reference Subjective File by Faisal Dar (MIT)

CS502 - Fundamentals of Algorithms [email protected]

23.Dijkstra algorithm correctness criteria two conditions. Pg158

Answer:

24.Floyd algorithm. Pg161

Answer:

25. A table is given and we have to make the adjacency list and matrix. page 116

Answer:

Page 8: CS502 Solved Reference Subjective File by Faisal Dar (MIT)

CS502 - Fundamentals of Algorithms [email protected]

26.An MST graph is given and we have to calculate its weight. Pg142

Answer:

27. Q1) answer the following according to Floyd Warshall 1) runing time 2)

space use

28.Write suedo code of dijkstra algorithm? 5marks. Pg156

Answer:

Page 9: CS502 Solved Reference Subjective File by Faisal Dar (MIT)

CS502 - Fundamentals of Algorithms

[email protected]

29.Make Adjacency list from the given table. 5marks

Answer:

Vertices/edges 1 2 3

1 1 1 1

2 0 0 1

3 0 1 0

Page 10: CS502 Solved Reference Subjective File by Faisal Dar (MIT)

CS502 - Fundamentals of Algorithms [email protected]

30. How generic algorithms work with minimum spanning tree 3marks

Answer:

31. Floyd-Warshall matrix diya tha os py 3 itration apply karny thy 5marks

Answer:

Page no 166

32.Which points should be supposed to prove the correctness of the Dijkstra's

Algorithm 3marks

Answer:

33. BFS pseudo code in graph (Page 12)

Answer:

Page 11: CS502 Solved Reference Subjective File by Faisal Dar (MIT)

CS502 - Fundamentals of Algorithms [email protected]

34. Given a graph run dijikstr's algorithm to find shortest path from vertex "S"

to all other vertices (5 marks) Page158

Answer:

Given a graph run prim's algorithm to find minimum spanning tree. Only show

the order of edges to be added to make spanning tree (5 marks)

Answer:

Page 12: CS502 Solved Reference Subjective File by Faisal Dar (MIT)

CS502 - Fundamentals of Algorithms

[email protected]

3- Given a graph run DFS and label time stamping (5 marks) pg149

Answer:

Page 13: CS502 Solved Reference Subjective File by Faisal Dar (MIT)

CS502 - Fundamentals of Algorithms [email protected]

35. What are the minimum and maximum numbers of elements in a heap of

height h (5 marks) Page no 44

Answer:

Minimum numbers=

Maximum numbers=

Page 14: CS502 Solved Reference Subjective File by Faisal Dar (MIT)

CS502 - Fundamentals of Algorithms [email protected]

36.Explain Floyd Warshell algorithm (3 marks)

Answer:

37. Define forward and back edges

Answer:

38.Compare bellman ford algorithm with dijikstr's algorithm. Also give the

time complexity of bellman ford algorithm (3 marks)

Answer:

A cycle in a digraph is a path containing at least one edge and for which v0 = vk.v0 is starting

vertices and vk is last vertices when both vertices are connected through edge then we can

consider it as a cycle. A Hamiltonian cycle is a cycle that visits every vertex in a graph exactly

once. An Eulerian cycle is a cycle that visits every edge of the graph exactly once.

Note: All the graphs are given from handouts but slightly different order of vertices and weights.

Page 15: CS502 Solved Reference Subjective File by Faisal Dar (MIT)

CS502 - Fundamentals of Algorithms [email protected]

Note: About 10 questions are from huffman encoding algorithm each of 1 mark

and much simple.

About 5 MCQ, s are from last chapter (Complexity theory) Other MCQ,s are also

from graph theory.

So 90% of the paper is from graph theory so prepare the chapter 8 (Graph theory)

of handouts with a great interest and specially all the graph algorithms (BFS, DFS,

Prim, Dijisktr's, Kruskal, Bellman ford, Floyd warshell etc)

39.RAM (Random Access memory) and its Applications? Pg10

Answer:

are executed one-by-one

Instructions

40. Describe Dijkstra’s algorithm working?

Answer:

41. Prim algorithm graph?

Page 17: CS502 Solved Reference Subjective File by Faisal Dar (MIT)

CS502 - Fundamentals of Algorithms [email protected]

42.Floyd-Warshall matrix complexity?

Answer:

IN floyd warshall we didnt allow G to have negative cost cycles.

43.Convert shortest path in to single source shortest path problem?

Answer:

44. Different between Average case and Worst case? Answer:

http://wiki.answers.com/Q/What_is_the_difference_between_best_worst_and_average_case

_complexity_of_an_algorithm

The worst case scenario, on the other hand, describes the absolute worst set of input for a given

algorithm. Let's look at a quick sort, which can perform terribly if you always choose the smallest or

largest element of a sub list for the pivot value. This will cause quick sort to

degenerate to O(n2).

Discounting the best and worst cases, we usually want to look at the average performance of an

algorithm. These are the cases for which the algorithm performs "normally."

45.Pseudo code algorithm for DFS Timestamp?

Answer:

Page 18: CS502 Solved Reference Subjective File by Faisal Dar (MIT)

CS502 - Fundamentals of Algorithms [email protected]

46.What is common problem in communication networks and circuit

designing? 2marks

Answer:

A common problem is communications networks and circuit design is that of connecting

together a set of nodes by a network of total minimum length. The length is the sum of lengths of

connecting wires.

47. Write pseudo code of relaxing a vertex 5

Answer:

Repeat

48. Define NP completeness (5marks) pg178

Answer:

NP-Complete L is NP-complete if

1. L ∈ NP and

2. L is NP-hard.

49.Define floyed warshall algorithm in these two cases (5marks)

Answer:

There are two types of cases in floyed warshall algorithm:

• do not go through a vertex k at all

• do go through a vertex k

Detail of these is

below:

50.Define DAG (marks3)

Answer:

A graph is said to be acyclic if it contains no cycles. A graph is connected if every vertex can

reach every other vertex. A directed graph that is acyclic is called a directed acyclic graph

(DAG)

51. Write steps of sieve techniques

Page 19: CS502 Solved Reference Subjective File by Faisal Dar (MIT)

CS502 - Fundamentals of Algorithms [email protected]

Answer:

52. Write Pseudo code of Dijkstra's algorithm

Answer:

REF PAGE :156

53. Prove the Lemma: Consider a diagraph G = ( V,E ) and any DFS forest for G.

G has a cycle if and only if the DFS forest has a back edges

Answer:

Page 20: CS502 Solved Reference Subjective File by Faisal Dar (MIT)

CS502 - Fundamentals of Algorithms [email protected]

54.Where the clique cover problem is used?

Answer:

55. What is decision problem, also explain with examples?

Answer:

A problem is called a decision problem if its output is a simple “yes” or “no” (or you may this of

this as

True/false, 0/1, accept/reject.) We will phrase an optimization problems as decision problems.

For example, the MST decision problem would be: Given a weighted graph G and an integer k,

does G have a spanning tree whose weight is at most k.

56.Let the adjacency list representation of an undirected graph is given.

Explain what property of the list indicates that the graph has an isolated

vertex.

As we know that An isolated vertex is a vertex with degree zero or a vertex that is not an

endpoint of any edge so in the given adjacency list the vertex is 0 and no end point of any edge

57. Analyze the brute force maxima algorithm

Page 21: CS502 Solved Reference Subjective File by Faisal Dar (MIT)

CS502 - Fundamentals of Algorithms [email protected]

Answer:

Page no 16-17

58.How Kruskal's algorithm works?

Answer:

Kruskal‟s algorithm worked by ordering the edges, and inserting them one by one into the

spanning tree, taking care never to introduce a cycle. Intuitively Kruskal‟s works by merging or

splicing two trees together, until all the vertices are in the same tree.4).

59.Given a matrix of graph G. IS G a directed or undirected graph?

Answer:

It is an Undirected Graph. In an undirected graph, we say that an edge is incident on a vertex if

the vertex is an endpoint of the edge.

60. What is the common problem in communication network and

circuit designing?

Answer:

Repeat

61. Differentiate between back edge and forward edge suppose you could prove

that an NP-complete problem cannot be solved in polynomial time. What would

be the consequence?

Answer:

Back Edge

A back edge connects a vertex to an ancestor in a DFS-tree. Note that a self-loop is a back edge.

from descendent to ancestor

(u, v) where v is an ancestor of u in the tree.

DFS tree may only have a single back edge A

back edge is an arc whose head dominates its tail (tail -> head) a

back edge must be a part of at least one loop

Page 22: CS502 Solved Reference Subjective File by Faisal Dar (MIT)

CS502 - Fundamentals of Algorithms [email protected]

Forward Edge

From ancestor to descendent

(u, v) where v is a proper descendent of u in the tree.

A forward edge is a non-tree edge that connects a vertex to a descendent in a DFS-tree.

Edge x-y is less than the capacity there is a forward edge x-y with a capacity equal to the

capacity and the flow

According to the question this means that the problem can be solved in Polynomial time using

known NP problem can be solved using the given problem with modified input (an NP problem

can be reduced to the given problem) then the problem is NP complete.

The main thing to take away from an NP-complete problem is that it cannot be solved in

polynomial time in any known way. NP-Hard/NP-Complete are a way of showing that certain

classes of problems are not solvable in realistic time.

Ref: Handouts Page No.128

62.Recursive explanation of dynamic programming.

Formulate the problem into smaller sub problems, find optimal solution to these sub

problems in a bottom up fashion then write an algorithm to find the solution of whole

problem starting with base case and works its way up to final solution.

Answer:

Dynamic programming is essentially recursion without repetition. Developing a dynamic

programming algorithm generally involves two separate steps:

Formulate problem recursively. Write down a formula for the whole problem as a simple

combination of answers to smaller sub problems.

Build solution to recurrence from bottom up. Write an algorithm that starts with base cases

and works its way up to the final solution.

Ref: Handouts Page No.75

63. What is the cost of the following graph?

Cost =33

Answer:

A common problem is communications networks and circuit design is that of connecting

together a set of nodes by a network of total minimum length. The length is the sum of lengths

of connecting wires.

Consider, for example, laying cable in a city for cable TV.

The computational problem is called the minimum spanning tree (MST) problem. Formally, we

are given a connected, undirected graph G = (V, E) each edge (u, v) has numeric weight of cost.

Page 23: CS502 Solved Reference Subjective File by Faisal Dar (MIT)

CS502 - Fundamentals of Algorithms

[email protected]

We define the cost of a spanning tree T to be the sum of the costs of edges in the spanning tree

w (T) = Σ w (u, v)

(u,v)2T

64. A minimum spanning tree is a tree of minimum weight The first is a

spanning tree but is not a MST;

Ref: Handouts Page No.142

65.Let the adjacency list representation of an undirected graph is given below.

Explain what general property of the list indicates that the graph has an

isolated vertex.

e◊ c ◊ b ◊a

d◊ a ◊b

f◊ e ◊ d ◊ a ◊c

f◊ c ◊ b ◊d

f◊ c ◊ a ◊e

e◊ d ◊ c ◊f

g

Answer:-

The main theorem which drives both algorithms is the following:

MST Lemma: Let G = (V, E) be a connected, undirected graph with real-valued weights on the

edges. Let A be a viable subset of E (i.e., a subset of some MST). Let (S, V − S) be any cut that

respects A and let (u, v) be a light edge crossing the cut. Then the edge (u, v) is safe for A.

MST Proof: It would simplify the proof if we assume that all edge weights are distinct. Let T be

any MST for G. If T contains (u, v) then we are done. This is shown in Figure 8.47 where the

lightest edge (u, v) with cost 4 has been chosen.

Ref: Handouts Page No.144

Floyd Warshal algorithm with three recursive steps

1. Wij = 0 if I = j

2. Wij = w(i,j) if i != j and (i,j) belongs to E

3. Wij = infinity if i != j and (i,j) not belongs to E

Answer

Page 24: CS502 Solved Reference Subjective File by Faisal Dar (MIT)

CS502 - Fundamentals of Algorithms [email protected]

The Floyd-Warshall algorithm: Step (i)

The Floyd-Warshall algorithm: Step (ii)

Recursively define the value of an optimal solution.

Boundary conditions: for k = 0, a path from vertex i to j with no intermediate vertex numbered

higher than 0 has no intermediate vertices at all, hence d (0)= wij

Recursive formulation:

is the solution for this APSP problem:

The Floyd-Warshall algorithm: Step (iii)

66. Compute the shortest-path weights bottom up FLOYD-WARSHALL(W, n)

Ref: Handouts Page No.161

Give a detailed example for 2d maxima problem

Answer: -

The problem with the brute-force algorithm is that it uses no intelligence in pruning out

decisions. For example, once we know that a point pi is dominated by another point pj, we do

not need to use pi for eliminating other points. This follows from the fact that dominance

relation is transitive. If pj dominates pi and pi dominates ph then pj also dominates ph; pi is not

needed. Ref: Handouts Page No.17

67. How the generic greedy algorithm operates in minimum spanning tree?

Answer: -

A generic greedy algorithm operates by repeatedly adding any safe edge to the current spanning

tree. When is an edge safe? Consider the theoretical issues behind determining whether an edge

is safe or not. Let S be a subset of vertices S _ V. A cut (S, V − S) is just a partition of vertices

into two disjoint subsets. An edge (u, v) crosses the cut if one endpoint is in S and the other is in

V − S. Given a subset of edges A, a cut respects A if no edge in A crosses the cut. It is not hard to

see why respecting cuts are important to this problem. If we have computed a partial MST and

we wish to know which edges can be added that do not induce a cycle in the current MST, any

edge that crosses a respecting cut is a possible candidate.

Ref: Handouts Page No.143

68. What are two cases for computing

Answer:-

There are two cases for computing Lij the match case if ai = bj , and the mismatch case if ai 6= bj

. In the match case, we do the following:

and in the mismatch case, we do the following:

Page 25: CS502 Solved Reference Subjective File by Faisal Dar (MIT)

CS502 - Fundamentals of Algorithms [email protected]

Ref: Handouts Page No.143

69.Describe Minimum Spanning Trees Problem with examples. Problem

Given a connected weighted undirected graph, design an algorithm that outputs a

minimum spanning tree (MST) of

Examples

The graph is a complete, undirected graph G = (V, E, W), where V is the set of pins, E is the set

of all possible interconnections between the pairs of pins and w (e) is the length of the wire

needed to connect the pair of vertices Ref: Handouts Page No.142

70.What is decision problem, also explain with example?

A decision problem is a question in some formal system A problem is called a decision problem

if its output is a simple “yes” or “no” (or you may this of this as

true/false, 0/1, accept/reject.) We will phrase may optimization problems as decision problems.

For example,

the MST decision problem would be: Given a weighted graph G and an integer k, does G have a

spanning tree whose weight is at most k?

Ref: Handouts Page No.170

71. Do you think greedy algorithm gives an optimal solution to the activity

scheduling

problem?

The greedy algorithm gives an optimal solution to the activity scheduling problem.

Proof:

The proof is by induction on the number of activities. For the basis case, if there are no activities,

then the greedy algorithm is trivially optimal. For the induction step, let us assume that the

greedy algorithm is optimal on any set of activities of size strictly smaller than |S| and we prove

the result for S. Let S0 be the set of activities that do not interfere with activity a1, That is any

solution for S0 can be made into a solution for S by simply adding activity a1, and vice versa.

Activity a1 is in the optimal schedule (by the above previous claim). It follows that to produce an

optimal schedule for the overall problem; we should first schedule a1 and then append the

optimal schedule for S0. But by induction (since |S0| < |S|), this is exactly what the greedy

Page 26: CS502 Solved Reference Subjective File by Faisal Dar (MIT)

CS502 - Fundamentals of Algorithms [email protected]

algorithm does. Ref: Handouts Page No.109

72. Define Forward edge

The most natural result of a depth first search of a graph (if it is considered as a function rather

than procedure) is a spanning tree of the vertices reached during the search. Based on this

spanning tree, the edges of the original graph can be divided into three classes: forward edges

(or "discovery edges"), which point from a node of the tree to one of its descendants,

Ref: Handouts Page No.129 130 73. Is there any relationship between number of back edges and number of

cycles in DFS?

Answer:

The time stamps given by DFS allow us to determine a number of things about a graph or

digraph. We can determine whether the graph contains any cycles.

Lemma: Given a digraph G = (V, E), consider any DFS forest of G and consider any edge (u, v) 2

E. If this edge is a tree, forward or cross edge, then f[u] > f[v]. If this edge is a back edge, then

f[u] _ f[v].

Proof: For the non-tree forward and back edges the proof follows directly from the parenthesis

lemma. For example, for a forward edge (u, v), v is a descendent of u and so v‟s start-finish

interval is contained within u‟s implying that v has an earlier finish time. For a cross edge (u, v)

we know that the two time intervals are disjoint. When we were processing u, v was not white

(otherwise (u, v) would be a tree edge), implying that v was started before u. Because the

intervals are disjoint, v must have also finished before u.

Ref: Handouts Page No.130

74. What is the common problem in communications networks and circuit

designing?

Answer:

A common problem is communications networks and circuit design is that of connecting

together a set of nodes by a network of total minimum length. The length is the sum of lengths of

connecting wires.

Consider, for example, laying cable in a city for cable TV.

Ref: Handouts Page No.142

Explain the following two basic cases according to Floyd-Warshall Algorithm,

1. Don t go through vertex k at all.

2. Do go through vertex k.

Answer:-

Don‟t go through k at all

Then the shortest path from i to j uses only intermediate vertices {1, 2, . . . , k − 1}. Hence the

length of

the shortest is d(k−1)

Page 27: CS502 Solved Reference Subjective File by Faisal Dar (MIT)

CS502 - Fundamentals of Algorithms

[email protected]

ij

Do go through k

First observe that a shortest path does not go through the same vertex twice, so we can assume

that we

pass through k exactly once. That is, we go from i to k and then from k to j. In order for the

overall path to be as short as possible, we should take the shortest path from i to k and the

shortest path from k to j.

Since each of these paths uses intermediate vertices {1, 2, . . . , k − 1}, the length of the path is

Ref: Handouts Page No.162

Show the result of time stamped DFS algorithm on the following graph. Take node

A as a starting node.

Answer:-

Depth-first search (DFS) :

It is an algorithm for traversing or searching a tree, tree structure, or graph. Intuitively, one

starts at the root (selecting some node as the root in the graph case) and explores as far as

possible along each branch before backtracking.

Formally, DFS is an uninformed search that progresses by expanding the first child node of the

search tree that appears and thus going deeper and deeper until a goal node is found, or until it

hits a node that has no children. Then the search backtracks, returning to the most recent node

it hadn't finished exploring. In a non-recursive implementation, all freshly expanded nodes are

added to a LIFO stack for exploration.

DFS (graph)

{

list L =empty

tree T =empty

choose a starting vertex x

search(x)

while(Lis not empty)

{

remove edge (v, w) from beginning of L

if w not yetvisited

{

add (v, w) to T

search(w)

}

}

}

search(vertex v)

{

visit v

for each edge (v, w)

add edge (v, w) to the beginning of L

}

Page 28: CS502 Solved Reference Subjective File by Faisal Dar (MIT)

CS502 - Fundamentals of Algorithms

[email protected]

-----------------------------------------------------------------------------------------------

Ref: http://www.chegg.com/homework-help/quest...omp-q96642

Why we need reduction?

Answer:-

The class NP-complete (NPC) problems consists of a set of decision problems (a subset of class

NP) that no one knows how to solve efficiently. But if there were a polynomial solution for even

a single NP-complete problem, then ever problem in NPC will be solvable in polynomial time.

For this, we need the concept of reductions.

Ref: Handouts Page No.173 Consider the digraph on eight nodes, labeled 1 through 8, with eleven directed

edges

1 2, 1 4, 2 4, 3 2, 4 5, 5 3 ,5 6, 7 8, 7 1, 2 7,8 7

Answer:-

0-7 0-1 1-4 1-6 2-3 3-4 4-2 5-2 6-0 6-3 6-5 7-1 7-3

Draw the DFS tree for the standard adjacency-matrix representation. List the

edges of each type in the space provided below.

0-7 tree 0

0-1 tree / \

1-4 tree 1 7

1-6 tree / \

2-3 tree 4 6

3-4 back | |

4-2 tree 2 5

5-2 cross |

6-0 back 3

6-3 cross

6-5 tree

7-1 cross

7-3 cross

Answer:

Ref http://www.cs.princeton.edu/courses/arch...1-sol.html Prove that the generic TRAVERSE (S) marks every vertex in any connected graph

exactly once and the set of edges (v, parent (v)) with parent (v) ¹F form a spanning

tree of the graph

Page 29: CS502 Solved Reference Subjective File by Faisal Dar (MIT)

CS502 - Fundamentals of Algorithms

[email protected]

Answer:-

Generic Traverse

• Suppose we want to visit every node in a connected graph (represented either explicitly or

implicitly)

• The simplest way to do this is an algorithm called depth-first search

• We can write this algorithm recursively or iteratively - it‟s the same both ways, the iterative

version just makes the stack explicit

• Both versions of the algorithm are initially passed a source vertex v

Lemma

Traverse(s) marks each vertex in a connected graph exactly once, and the set of edges (v,

parent(v)), with parent(v) not nil, form a spanning tree of the graph. Proof

• Call an edge (v, parent(v)) with parent(v) =6 nil a parent edge.

• Note that since every node is marked, every node has a parent edge except for s.

• It now remains to be shown that the parent edges form a spanning tree of the graph

Ref http://www.cs.unm.edu/~saia/362-s08/lec/lec18-2x2.pdf

What is a run time analysis and its two criteria

Answer:-

The main purpose of our mathematical analysis will be measuring the execution time. The

running time of an implementation of the algorithm would depend upon the speed of the

computer, programming language, optimization by the compiler etc. Two criteria for measuring

running time are worst-case time and average-case time.

Worst-case time is the maximum running time over all (legal) inputs of size n. Let I denote an

input instance, let |I| denote its length, and let T(I) denote the running time of the algorithm on

input I. Then

Average-case time is the average running time over all inputs of size n. Let p(I) denote the

probability of seeing this input. The average-case time is the weighted sum of running times

with weights being the probabilities:

We will almost always work with worst-case time. Average-case time is more difficult to

compute; it is difficult to specify probability distribution on inputs. Worst-case time will specify

an upper limit on the running time.

Ref: Handouts Page No.173

Dijkstra algorithm correctness criteria two conditions

Answer:

We will prove the correctness of Dijkstr‟s algorithm by Induction. We will use the definition that

_(s, v)

denotes the minimal distance from s to v.

For the base case

Page 30: CS502 Solved Reference Subjective File by Faisal Dar (MIT)

CS502 - Fundamentals of Algorithms

[email protected]

1. S = {s}

2. d(s) = 0, which is _(s, s

Ref: Handouts Page No.158 Compare bellman ford algorithm with dijikstr's algorithm. Also give the time

complexity of bellman ford algorithm (3 marks)

Answer:

Dijkstra‟s single-source shortest path algorithm works if all edges weights are non-negative and

there are no negative cost cycles. Bellman-Ford allows negative weights edges and no negative

cost cycles. The algorithm is slower than Dijkstra‟s, running in _(VE) time. Like Dijkstra‟s

algorithm, Bellman-Ford is based on performing repeated relaxations. Bellman-Ford applies

relaxation to every edge of the graph and repeats this V − 1 times.

Ref: Handouts Page No.159

psedo code of timestamp DFS

Answer:

DFS(G)

1 for (each u 2 V)

2 do color[u] white

3 pred[u] nil

4 time 0

5 for each u 2 V

6 do if (color[u] = white)

7 then DFSVISIT(u)

Ref: Handouts Page No.126

variants of shortest path solution

Answer

There are a few variants of the shortest path problem.

Single-source shortest-path problem: Find shortest paths from a given (single) source vertex s 2

V to every other vertex v 2 V in the graph G.

Single-destination shortest-paths problem: Find a shortest path to a given destination vertex t

from each vertex v. We can reduce the this problem to a single-source problem by reversing the

direction of each edge in the graph.

Single-pair shortest-path problem: Find a shortest path from u to v for given vertices u and v. If

we solve the single-source problem with source vertex u, we solve this problem also. No

algorithms for this problem are known to run asymptotically faster than the best single-source

algorithms in the worst case.

All-pairs shortest-paths problem: Find a shortest path from u to v for every pair of vertices u and

v. Although this problem can be solved by running a single-source algorithm once from each

vertex, it can usually be solved faster.

Ref: Handouts Page No.153

Page 31: CS502 Solved Reference Subjective File by Faisal Dar (MIT)

CS502 - Fundamentals of Algorithms

[email protected]

Prove the following lemma,

Lemma: Given a digraph G = (V, E), consider any DFS forest of G and consider any

edge (u, v) ∈ E. If this edge is a tree, forward or cross edge, then f[u] > f[v]. If this

edge is a back edge, then f[u] ≤ f[v]

Answer:-

Proof: For the non-tree forward and back edges the proof follows directly from the parenthesis

lemma. For example, for a forward edge (u, v), v is a descendent of u and so v‟s start-finish

interval is contained within u‟s implying that v has an earlier finish time. For a cross edge (u, v)

we know that the two time intervals are disjoint. When we were processing u, v was not white

(otherwise (u, v) would be a tree edge), implying that v was started before u. Because the

intervals are disjoint, v must have also finished before u.

RAM(Random Access memory)and its Applications?

Answer:

A RAM is an idealized machine with an infinitely large random-access memory. Instructions are

executed one-by-one (there is no parallelism). Each instruction involves performing some basic

operation on two values in the machines memory. Basic operations include things like assigning

a value to a variable, computing any basic arithmetic operation (+, - , × , integer division) on

integer values of any size, performing any comparison (e.g. x _

Ref: Handouts Page No.10

Describe Dijkstra’s algorithm working?

Answer

Dijkstra‟s algorithm works on a weighted directed graph G = (V, E) in which all edge weights are

non-negative, i.e., w(u, v) _ 0 for each edge (u, v) 2 E.

Ref: Handouts Page No.154

Prim algorithm graph?

Answer

Prim‟s algorithm builds the MST by adding leaves one at a time to the current tree. We start with

a root vertex r; it can be any vertex. At any time, the subset of edges A forms a single tree. We

look to add a single vertex as a leaf to the tree.

Ref: Handouts Page No.149

write suedo code of relaxing a vertex 5

Answer

RELAX((u, v))

1 if (d[u] + w(u, v) < d[v])

2 then d[v] d[u] + w(u, v)

3 pred[v] = u

Ref: Handouts Page No.155

Define NP completeness

Answer:

The set of NP-complete problems is all problems in the complexity class NP for which it is

known that if anyone is solvable in polynomial time, then they all are. Conversely, if anyone is

not solvable in polynomial time, then none are.

Page 32: CS502 Solved Reference Subjective File by Faisal Dar (MIT)

CS502 - Fundamentals of Algorithms

[email protected]

Definition: A decision problem L is NP-Hard if

L0 _P L for all L0 2 NP.

Definition: L is NP-complete if

1. L 2 NP and

2. L0 _P L for some known NP-complete problem L0.

Ref: Handouts Page No.176

Define DAG

A directed graph that is acyclic is called a directed acyclic graph (DAG).

Ref: Handouts Page No.116 How Kruskal's algorithm works ?

Answer:

Kruskal‟s algorithm works by adding edges in increasing order of weight (lightest edge first). If

the next edge does not induce a cycle among the current set of edges, then it is added to A. If it

does, we skip it and consider the next in order. As the algorithm runs, the edges in A induce a

forest on the vertices. The trees of this forest are eventually merged until a single tree forms

containing all vertices.

Ref: Handouts Page No.147

What are two steps generally involved while developing a dynamic programming

algorithm?

Answer:

Dynamic programming is essentially recursion without repetition. Developing a dynamic

programming algorithm generally involves two separate steps:

Formulate problem recursively. Write down a formula for the whole problem as a simple

combination of answers to smaller sub problems.

Build solution to recurrence from bottom up. Write an algorithm that starts with base cases and

works its way up to the final solution.

Ref: Handouts Page No.75

The following adjacency matrix represents a graph that consists of four vertices

labeled 0, 1, 2 and 3. The entries in the matrix indicate edge weights.

0 1 2 3

0 0 1 0 3

1 2 0 4 0

2 0 1 0 1

3 2 0 0 0

Answer

NO. Because number of rows and number of columns are always same.

What is the application of edit distance technique?

Answer

Edit Distance: Applications

There are numerous applications of the Edit Distance algorithm. Here are some

examples:

Page 33: CS502 Solved Reference Subjective File by Faisal Dar (MIT)

CS502 - Fundamentals of Algorithms [email protected]

Spelling Correction

If a text contains a word that is not in the dictionary, a „close‟ word, i.e. one with a small edit

distance, may be suggested as a correction. Most word processing applications, such as

Microsoft Word, have spelling checking and correction facility. When Word, for example, finds

an incorrectly spelled word, it makes suggestions of possible replacements.

Plagiarism Detection

If someone copies, say, a C program and makes a few changes here and there, for example,

change variable names, add a comment of two, the edit distance between the source and copy

may be small. The edit distance provides an indication of similarity that might be too close in

some situations.

Computational Molecular Biology

Speech Recognition

Algorithms similar to those for the edit-distance problem are used in some speech recognition

systems. Find a close match between a new utterance and one in a library of classified

utterances.

Ref: Handouts Page No.76

Fibonacci sequence? 2mark

Answer:

Ref : Page no 73

Communication design problem (MST). Answer:

Ref: Page no 142 Strong connected component problem Answer:

Ref: Page no 135