Ads Answersheet
Ads Answersheet
Ads Answersheet
Ans. The Robin-Karp algorithm is a pattern matching algorithm that uses hashing to
find occurrences of a pattern within a larger text. It works by comparing the hash
values of the pattern and substrings of the text.
Ans. The all pair shortest path algorithm is also known as Floyd-Warshall algorithm is used
to find all pair shortest path problem from a given weighted graph. As a result of
this algorithm, it will generate a matrix, which will represent the minimum distance from
any node to all other nodes in the graph.
At first the output matrix is same as given cost matrix of the graph. After that the output
matrix will be updated with all vertices k as the intermediate vertex.
The time complexity of this algorithm is O(V3), here V is the number of vertices in the graph.
036∞∞∞∞
3021∞∞∞
620142∞
∞1102∞4
∞∞42021
∞∞2∞201
∞∞∞4110
0345677
3021344
4201323
5110233
6332021
7423201
7433110
Examples
Step 1
Initialize the distances of all the vertices as ∞, except the source node S.
Vertex S A B C D E
Distance 0 ∞ ∞ ∞ ∞ ∞
Now that the source vertex S is visited, add it into the visited array.
visited = {S}
Step 2
The vertex S has three adjacent vertices with various distances and the
vertex with minimum distance among them all is A. Hence, A is visited and
the dist[A] is changed from ∞ to 6.
S→A=6
S→D=8
S→E=7
Vertex S A B C D E
Distance 0 6 ∞ ∞ 8 7
Visited = {S, A}
Step 3
There are two vertices visited in the visited array, therefore, the adjacent
vertices must be checked for both the visited vertices.
Vertex S has two more adjacent vertices to be visited yet: D and E. Vertex
A has one adjacent vertex B.
S → D = 8 and S → E = 7.
S → B = S → A + A → B = 6 + 9 = 15
Vertex S A B C D E
Distance 0 6 15 ∞ 8 7
Visited = {S, A, E}
Step 4
S→D=8
S → B = 15
S → C = S → E + E → C = 7 + 5 = 12
Vertex S A B C D E
Distance 0 6 15 12 8 7
Visited = {S, A, E, D}
Step 5
S → B = S → A + A → B = 6 + 9 = 15
S → B = S → D + D → C + C → B = 8 + 3 + 12 = 23
Vertex S A B C D E
Distance 0 6 15 11 8 7
Visited = { S, A, E, D, C}
Step 6
The remaining unvisited vertex in the graph is B with the minimum distance
15, is added to the output spanning tree.
Visited = {S, A, E, D, C, B}
The shortest path spanning tree is obtained as an output using the dijkstra’s
algorithm.
➢ Explain the representation of graph in the memory of a computer? Campare the
graph representation method.
To represent a graph, we just need the set of vertices, and for each vertex the neighbors of the vertex
(vertices which is directly connected to it by an edge). If it is a weighted graph, then the weight will
be associated with each edge.
There are different ways to optimally represent a graph, depending on the density of its edges, type
of operations to be performed and ease of use.
1. Adjacency Matrix
o It is used to represent which nodes are adjacent to each other. i.e. is there any edge connecting nodes
to a graph.
o In this representation, we have to construct a nXn matrix A. If there is any edge from a vertex i to
vertex j, then the corresponding element of A, a i,j = 1, otherwise ai,j= 0.
Note, even if the graph on 100 vertices contains only 1 edge, we still have to have a 100x100
matrix with lots of zeroes.
o If there is any weighted graph then instead of 1s and 0s, we can store the weight of the edge.
Example
Consider the following undirected graph representation:
In the above examples, 1 represents an edge from row vertex to column vertex, and 0 represents no
edge from row vertex to column vertex.
Cons: It takes a lot of space and time to visit all the neighbors of a vertex, we have to traverse all the
vertices in the graph, which takes quite some time.
2. Adjacency List
o In this representation, for each vertex in the graph, we maintain the list of its neighbors. It means,
every vertex of the graph contains list of its adjacent vertices.
o We have an array of vertices which is indexed by the vertex number and for each vertex v, the
corresponding array element points to a singly linked list of neighbors of v.
Example
Let's see the following directed graph representation implemented using linked list:
Pros:
o Such kind of representation is easy to follow and clearly shows the adjacent nodes of node.
Cons:
o The adjacency list allows testing whether two vertices are adjacent to each other but it is slower to
support this operation.
➢ Kmp string matching algorithm.
Ans. Knuth-Morris and Pratt introduce a linear time algorithm for the string matching problem. A
matching time of O (n) is achieved by avoiding comparison with an element of 'S' that have
previously been involved in comparison with some element of the pattern 'p' to be matched. i.e.,
backtracking on the string 'S' never occurs
Let us execute the KMP Algorithm to find whether 'P' occurs in 'T.'
For 'p' the prefix function, ? was computed previously and is as follows:
Solution:
Initially: n = size of T = 15
m = size of P = 7
Pattern 'P' has been found to complexity occur in a string 'T.' The total number of shifts that took
place for the match to be found is i-m = 13 - 7 = 6 shifts.
1, Identify string.
Ans. Given a string str, the task is to check if the string is a valid identifier or not. In order to
qualify as a valid identifier, the string must satisfy the following conditions:
1.It must start with an either underscore(_) or any of the characters from the ranges [‘a’,
‘z’] and [‘A’, ‘Z’].
2.There must not be any white space in the string.
3.And, all the subsequent characters after the first character must not consist of any special
characters like $, #, % etc.
As we have discussed, one graph may have more than one spanning tree. If there
are n number of vertices, the spanning tree should have n - 1 number of edges. In this
context, if each edge of the graph is associated with a weight and there exists more than
one spanning tree, we need to find the minimum spanning tree of the graph.
Moreover, if there exist any duplicate weighted edges, the graph may have multiple
minimum spanning tree.
In the above graph, we have shown a spanning tree though it’s not the minimum spanning
tree. The cost of this spanning tree is (5 + 7 + 3 + 3 + 5 + 8 + 3 + 4) = 38.
3. Define string cantentation.
Ans.
4. Maximum flow.
Ans. The Ford-Fulkerson algorithm is used to detect maximum flow from start vertex to
sink vertex in a given graph. In this graph, every edge has the capacity. Two vertices are
provided named Source and Sink. The source vertex has all outward edge, no inward edge,
and the sink will have all inward edge no outward edge.
There are some constraints:
Ans.