Cs 502 Final Reference Subjective S
Cs 502 Final Reference Subjective S
com
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).
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:
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.
22
333
…….
……….
n n n n ……. n (n times)
Answer: http://answers.yahoo.com/question/index?qid=1006011700253
CS502 - Fundamentals of Algorithms
[email protected]
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:
22.What are a run time analysis and its two criteria? pg13
Answer:
CS502 - Fundamentals of Algorithms [email protected]
25. A table is given and we have to make the adjacency list and matrix. page 116
Answer:
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
Vertices/edges 1 2 3
1 1 1 1
2 0 0 1
3 0 1 0
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:
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:
CS502 - Fundamentals of Algorithms
[email protected]
3- Given a graph run DFS and label time stamping (5 marks) pg149
Answer:
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=
CS502 - Fundamentals of Algorithms [email protected]
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.
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)
Instructions
are executed one-by-one
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."
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.
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
Page no 16-17
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
CS502 - Fundamentals of Algorithms [email protected]
Forward Edge
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
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
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.
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.
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
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:-
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
Answer
CS502 - Fundamentals of Algorithms [email protected]
Recursive formulation:
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
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:
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
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
CS502 - Fundamentals of Algorithms [email protected]
algorithm does. Ref: Handouts Page No.109
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
Since each of these paths uses intermediate vertices {1, 2, . . . , k − 1}, the length of the path is
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
}
CS502 - Fundamentals of Algorithms
[email protected]
-----------------------------------------------------------------------------------------------
Ref: http://www.chegg.com/homework-help/quest...omp-q96642
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
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
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
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
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
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.
Define DAG
A directed graph that is acyclic is called a directed acyclic graph (DAG).
Ref: Handouts Page No.116
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.
0123
00103
12040
20101
32000
Answer
NO. Because number of rows and number of columns are always same.
Ref : Page no 73