Вы находитесь на странице: 1из 12

C:\Users\RAHUL\Documents\graphquestions.

txt

Tuesday, January 18, 2011 7:15 PM

Graphs Interview Questions Part 1


10:10 AM Posted by ctsasikumar
********1.Any search algorithm that considers outgoing edges of a vertex before any neighbors
of the vertex, that is, outgoing edges of the vertex's predecessor in the search, where
extremes are searched first is called
a.Depth first search
b.Breadth first seach
c.Prims algorithm
d.Kruskals algorithm
Ans:a
************2. Depth first search can be implemented with recursion.(T/F)
Ans:True
*******************3. A search algorithm that considers neighbors of a vertex, that is,
outgoing edges of the vertex's predecessor in the search, before any outgoing edges of the
vertex, where extremes are searched last is called
a.Depth first search
b.Breadth first seach
c.Prims algorithm
d.Kruskals algorithm
Ans:b
4. Depth first search can be implemented with
a.stack
b.queue
c.tree
d.priority queue
Ans:a
5. Breadth first search can be implemented with
a.stack
b.queue
c.tree
d.forest
Ans:b
******6. For tree search, depth first search tends to require_____ memory, as compared to
breadth first search.
a.less
b.more
c.same
d.cannot say
Ans:a
For tree search at least, depth first search tends to require less memory, as you only need to
record nodes on the `current' path. If there are lots of solutions, but all at a comparable
`depth' in the tree, then you may hit on a solution having only explored a very small part of
the tree.
7. __________ may use more memory, but will not get stuck in blind alleys, and will always find
the shortest path first (or at least, the path that involves the least number of steps).
a.Depth first search
-1-

C:\Users\RAHUL\Documents\graphquestions.txt

Tuesday, January 18, 2011 7:15 PM

b.Breadth first seach


c.Prims algorithm
d.Kruskals algorithm
Ans:b
Breadth first search may use more memory, but will not get stuck in blind alleys, and will
always find the shortest path first (or at least, the path that involves the least number of
steps). It may be more appropriate when exploring very large search spaces where there is an
expected solution which takes a relatively small number of steps, or when you are interested in
all the solutions (perhaps up to some depth limit).
8.What is the output of pre-order traversal using DFS for the given graph

a. 1 2 3 4 8 7 5 6 b. 1 2 3 4 7 8 6 5 c.1 2 3 4 8 7 6 5 d. 1 2 3 4 8 6 7 5
Ans:c
http://www.cs.duke.edu/csed/jawaa/DFSanim.html

9. What is the output of post-order traversal using DFS for the given graph

a. 2 7 8 5 6 4 1 3 b.2 7 8 5 6 4 3 1 c. 2 7 8 5 4 6 3 1 d. 2 7 5 8 6 4 3 1
Ans:b
Graphs Interview Questions Part 2
10:11 AM Posted by ctsasikumar
10. For the following graph:

a depth-first search starting at A, assuming that the left edges in the shown graph are chosen
before right edges, and assuming the search remembers previously-visited nodes and will not
repeat them, will visit the nodes in the following order:
a. A, B, D, F, E, C, G b. A, B, C, F, E, D, G c. A, B, D, E, F, C, G d. A, B, D, F, E, G, C
Ans:a
11. For the following graph:

Performing the depth first search without remembering previously visited nodes results in
visiting nodes in the order
a. B , A, D, F, E, B , A , D, F, E, etc. forever
b. A, D,B, F, E, A, D , B , F, E, etc. forever
c. A, B, D, E, F, A, B, D, E , F etc. forever
d. A, B, D, F, E, A, B, D, F, E, etc. forever
Ans:d
Performing the same search without remembering previously visited nodes results in visiting
nodes in the order A, B, D, F, E, A, B, D, F, E, etc. forever, caught in the A, B, D, F, E
cycle and never reaching C or G.
12. code(graph G)
{
list L = empty
tree T = empty
-2-

C:\Users\RAHUL\Documents\graphquestions.txt

Tuesday, January 18, 2011 7:15 PM

choose a starting vertex x


search(x)
while(L is not full)
remove edge (v, w) from end of L
if w not yet visited
{
add (v, w) to T
search(w)
}
}
search(vertex v)
{
visit v
for each edge (v, w)
add edge (v, w) to end of L
}
The code here can represent
a.Depth first search
b.Breadth first seach
c.Prims algorithm
d.Kruskals algorithm
Ans:a Level:3
13. function (Start, Goal) {
Push(Stack,Start)
while notEmpty(Stack) {
Node := Pop(Stack)
if Node = Goal {
return Node /*the code below does not get executed*/
}
for each Child in Expand(Node) {
if notVisited(Child) {
setVisited(Child)
Push(Stack, Child)
}
}
}
}
The function here can represent
a.Depth first search
b.Breadth first seach
c.Prims algorithm
d.Kruskals algorithm
Ans:a Level:3
14.The following algorithm represents
1. Put the starting node (the root node) in the queue.
2. Pull a node from the beginning of the queue and examine it.
o If the searched element is found in this node, quit the search and return a result.
o Otherwise push all the (so-far-unexamined) successors of this node into the end of the queue,
if there are any.
3. If the queue is empty, every node on the graph has been examined -- quit the search and
-3-

C:\Users\RAHUL\Documents\graphquestions.txt

Tuesday, January 18, 2011 7:15 PM

return "not found".


4. repeat from step 2.
a.Depth first search
b.Breadth first seach
c.Prims algorithm
d.Kruskals algorithm
Ans:b
15. function (Start, Goal) {
enqueue(Queue,Start)
while notEmpty(Queue) {
Node := dequeue(Queue)
if Node = Goal {
return Node /*the code below does not get executed*/
}
for each Child in Expand(Node) {
if notVisited(Child) {
setVisited(Child)
enqueue(Queue, Child)
}
}
}
}
The function here can represent
a.Depth first search
b.Breadth first seach
c.Prims algorithm
d.Kruskals algorithm
Ans:b
******16. The immense demand for space is the reason why breadth-first search is impractical
for larger problems(T/F)
Ans:True
***************17. Space complexity of breadth-first search where v is the no. of nodes and e
is the no. of edges is
a.O(|V| + |E|)
b. O(|E|)
c. O(|V| )
d. O(|V| log |E|)
Ans:a
Since all nodes discovered so far have to be saved, the space complexity of breadth-first
search is O(|V| + |E|) where |V| is the number of nodes and |E| the number of edges in the graph
*********************Graphs Interview Questions Part 3
10:12 AM Posted by ctsasikumar
18. Time complexity of breadth-first search where v is the no. of nodes and e is the no. of
edges is
a.O(|V| + |E|)
b. O(|E|)
c. O(|V| )
d. O(|V| log |E|)
Ans:a
Since in the worst case breadth-first search has to consider all paths to all possible nodes
-4-

C:\Users\RAHUL\Documents\graphquestions.txt

Tuesday, January 18, 2011 7:15 PM

the time complexity of breadth-first search is O(|V| + |E|) where |V| is the number of nodes
and |E| the number of edges in the graph.
##########19. Applications of BFS are
a.Finding all connected components in a graph.
b.Finding all nodes within one connected component
c.Finding the shortest path between two nodes u and v (unweighted nodes)
d.All the above
Ans:d
20.For the following graph, using breadth first search, which of the following elements are
present at level 0
a.1,2,3 b.1 c.1,2 d.2,3
Ans:b
21. For the following graph, using breadth first search, which of the following elements are
present at level 1
a.1,2,3 b.1 c.1,2 d.2,3
Ans:d
22. For the following graph, using breadth first search, which of the following elements are
present at level 2
a.1,2,3 b.4,5 c.3,4,5 d.2,3
Ans:b
Saturday, October 27, 2007
Graphs Interview Questions Part 4
10:13 AM Posted by ctsasikumar
23. For the following graph, using breadth first search, which of the following elements are
present at level 3
a.8,6 b.4,5 c.8,7,6 d.2,3
Ans:a
24. For the following graph, using breadth first search, which of the following elements are
present at level 4
a.8,7,6 b.4,5 c.8,6 d.7
Ans:d
***25. Depth first search is another way of traversing graphs, which is closely related to
preorder traversal of a tree.(T/F)
Ans:True
26.Choose the right statement from the following
******a.Depth first search is closely related to traveling salesman problem
b. Breadth first search is closely related to traveling salesman problem
c. Breadth first search can be thought of as being like Dijkstra's algorithm for shortest paths
d. Depth first search can be thought of as being like Dijkstra's algorithm for shortest paths
Ans:a,c
27. Depth First Search is a generalization of the ________.
a.Inorder traversal
bPreorder traversal
c.Postorder traversal
d.none
Ans:b
-5-

C:\Users\RAHUL\Documents\graphquestions.txt

Tuesday, January 18, 2011 7:15 PM

*****28. For a graph with n nodes to be Cyclic, the minimum number of edges required are________.
a.n/2
b.n+1
c.n-1
d.n
Ans:d
*********29. For a Graph with v nodes and e edges, the running time of DFS is given by ________.
a.O(|V| + |E|)
b. O(|E|)
c. O(|V| )
d. O(|V| log |E|)
Ans:a
30. Breadth First Search algorithm can be used in Prim's MST algorithm. (T/F)
Ans:True
31. Breadth First Search algorithm cannot be used in Dijkstras single source shortest path
algorithm. (T/F)
Ans:False
************32. A graph with V vertices and E edges can be represented with an adjacency matrix
using O(V2) space. (T/F)
true.
33. Consider the following pseudo code.
void PQR(Vertex v)
{ v.visited = true ;
For each w adjacent to v
if (!w.visited)
PQR(w); }
Which of the following is/are it intended to do?
a. Finding the adjacency matrix
b. Depth first search
c. Finding the path matrix
d. Breadth first search
e. Calculating the shortest path
Ans. Depth first search
34. Depth first search is a generalization of
a. Inorder traversal
b. Preorder traversal
c. Postorder traversal
d. none of the above
Ans. b
35. Traversing a graph by visiting all the nodes attached directly to a starting node first is
called ___________
a. Depth-first search
b. Breadth-first search
c. Adjacency list
d. Adjacency matrix
Ans. b
Graphs Interview Questions Part 5
10:14 AM Posted by ctsasikumar
36. A structure for representing a graph in which the presence of arcs between nodes is is
-6-

C:\Users\RAHUL\Documents\graphquestions.txt

Tuesday, January 18, 2011 7:15 PM

indicated by an entry in a matrix is ________


a. Breadth-first search
b. Depth-first search
c. Adjacency matrix
d. Adjacency list
Ans. c
37. A structure for representing a graph in which the arcs are stored as lists of connections
between nodes is ________
a. Breadth-first search
b. Depth-first search
c. Adjacency matrix
d. Adjacency list
Ans. d
**********38. The amount of space required to store an adjacency-matrix is ____ where V is a
vertex set whose elements are vertices.
a. O(V)
b. O(V+E)
c. O(V)
d. O(V*E)
Ans. c
******************39. Any edge in an adjacency matrix representation can be accessed,added or
removed in ______ time.
a. O(V)
b. O(1)
c. O(E)
d. O(V)
Ans. b
***************40. For sparse graphs,the amount of memory required to store an adjacency list
is ______
a. O(V)
b. O(V)
c. O(V+E)
d. O(V*E)
Ans. c
*************###########41. A technique that picks the next adjacent unvisited vertex until
reaching a vertex that has no unvisited adjacent vertices is ________
a. Breadth-first search
b. Depth-first search
c. Adjacency matrix
d. Adjacency list
Ans. b
42. Consider the graph represented by the following adjacency list:
1: 236
2: 513
3: 21756
4: 57
5: 2436
-7-

C:\Users\RAHUL\Documents\graphquestions.txt

Tuesday, January 18, 2011 7:15 PM

6: 351
7: 34
Perform a Breadth First Search in the graph starting from node 1 and processing the
edges adjacent to a node in the order they appear in the adjacency list.
What is the order in which the nodes are visited?
a) 1,2,3,6,4,7,5
b) 1,2,3,6,7,5,4
c) 1,2,3,6,5,4,7
d) 1,2,3,6,5,7,4
e) 1,2,3,6,7,4,5
Ans. d
44. If you perform a Depth First Search in a binary tree, what traversal will you obtain?
a) pre-order
b) in-order
c) post-order
d) Eulerian
Ans. a
*********45. Given a graph G with n nodes, you want to find the node that has maximum degree.
What would be the complexity using an adjacency matrix?
a) O(1) b) O(log n) c) O(n) d) O(n log n) e) O(n2)
Ans. e
***************###############46. Given a graph G with n nodes and m edges, you want to find
all the nodes with
degree 5. What would be the complexity using an adjacency list (where the degree of
each node is not stored)?
a) O(n) b) O(log m) c) O(log n) d) O(m) e) O(n logn )
Ans. d
**************47. Which of the following statements is incorrect?
a) A tree with n nodes has n-1 edges
b) Dijkstra does not work if some weights are negative
*************c) BSF finds whether a graph is connected
d) All nodes in a graph must have degree at least 1
Ans. d
*************48.Depth first search of a graph is
i.recursive
ii.can be parallelized
a.only I b.only ii c.i and ii d.neither I nor ii
Ans:a
Some problems have no parallel algorithms, and are called inherently serial problems. Those
problems cannot be solved faster by employing more processors. One such example is depth-first
search of a graph, which happens to be recursive, but can not be parallelized

Graphs Interview Questions Part 6


10:14 AM Posted by ctsasikumar
************49. Determining the shortest path from every vertex to every other vertex in a
weighted graph (with non-negative weights) using Dijkstra's algorithm takes O(V3) time. (T/F)
Ans. True -- determining the shortest path from a single vertex s to every other vertex takes
-8-

C:\Users\RAHUL\Documents\graphquestions.txt

Tuesday, January 18, 2011 7:15 PM

O(V2) time. So determining the shortest path for every vertex will take O(V3) time.
*********51. Which one of the following is a faster technique to test if (x,y) is in a graph?
a. adjacency list
b. adjacency matrix
c. Kruskals algorithm
d. Dijkstras algorithm
Ans. b
***************52. Adjacency lists are faster to find the degree of a vertex as compared to
adjacency matrix. True or False
Ans. True
53. ________ is a graph representation that occupies less memory on small graphs.
a. forest
b. adjacency list
c. adjacency matrix
d. tree
***********Ans. b because for adjacency list its (m+n) whereas for matrix its n^2.
***********54. _________ is a graph representation that occupies less memory on big graphs.
a. adjacency matrix
b. adjacency list
c. forest
d. queue
Ans. a
***************###########33*55. Faster traversal in a graph is possible in which representation?
a. forest
b. adjacency list
c. adjacency matrix
d. tree
Ans. b
adjacency lists vs. adjacency matrix
***********************56. Edge insertion or deletion is better in adjacency matrices as
compared to adjacency list. (True or False)
Ans. True
adjacency matrices O(1) vs. O(d)
57. Given the graph, the depth-first traversal will be
a. ACGBEFHD
b. ABCDEFGH
c. ACDBEFHG
d. ACGDEFBH
Ans. a
58. Given the following graph,the resulting depth-first traversal will be

a. BEFGHCAD b. BFEGHCAD c. BEGHFCAD d. BEFHGCAD


Ans. a
Refer Pg-565 for the resulting tree (Data Structures Using C and C++ by Tanenbaum)
-9-

C:\Users\RAHUL\Documents\graphquestions.txt

Tuesday, January 18, 2011 7:15 PM

********************59. Depth-first traversal method in a graph


i. creates a spanning forest
ii. can be used to determine if an undirected graph is connected
iii. to identify the connected components of an undirected graph
The correct options are
a. i only
b. i and ii only
c. ii only
d. i,ii and iii
Ans. d
***********###############360. Using adjacency matrix representation,the efficiency of
depth-first search is
a. O(n+n) or O(n)
b. O(e)
c. O(n)
d. O(n+e)
Where n is the number of graph nodes and e is the number of edges in the graph.
Ans. a
Refer Pg-573 of Data Structures in c and C++ by tanenbaum
*************61. Using adjacency list representation,the efficiency of depth-first traversal is
a. O(n+n)
b. O(e)
c. O(n)
d. O(n+e)
Ans. d
Traversing all successors of all nodes is O(e) and assuming that the graph nodes are organized
as an array or a linked list ,visiting all n nodes is O(n).hence,efficiency is O(n+e).
62. Given the graph ,the resulting breadth-first traversal will be

a. ABCDEFGH
b. ACDGBFEH
c. ACDBEFGH
d. ACDGBEFH
Ans. b
Refer pg-565 fig.b of Data Structures Using C and C++ by Tanenbaum
*********************#########63. Using adjacency matrix representation,the efficiency of
breadth-first search is
a. O(n+n) or O(n)
b. O(e)
c. O(n)
d. O(n+e)
Where n is the number of graph nodes and e is the number of edges in the graph
Ans. a
***************64. Using adjacency list representation,the efficiency of breadth-first
traversal is
-10-

C:\Users\RAHUL\Documents\graphquestions.txt

Tuesday, January 18, 2011 7:15 PM

a. O(n+n)
b. O(e)
c. O(n)
d. O(n+e)
Ans. d
Traversing all successors of all nodes is O(e) and assuming that the graph nodes are organized
as an array or a linked list ,visiting all n nodes is O(n).hence,efficiency is O(n+e).

*********65. The adjacency matrix representation size is equal to the number of nodes available
in the graph.(T/F)
Ans:True
66.A weighted graph has a weight/cost of construction for
a.no edge
b.1 edge
c.2 edges
d.each edge
Ans:d
67.The adjacency matrix representation mat[i][j] has a value ____ when there is an edge from
ith to jth node
a.0
b.1
c.2
d.can be any value
Ans:b
*************68. The adjacency matrix representation mat[i][j] has a value ____ when there is
no edge from ith to jth node
a.0
b.1
c.2
d.can be any value
Ans:a
69. If there is a solution breadth-first search will find it regardless of the kind of graph.
However, if the graph is infinite and there is no solution breadth-first search will
diverge.What does this property mean?
a. BFS is complete
b. BFS is incomplete
c. BFS is optimal
d. BFS implements queue
Ans. a
*********Breadth-first search is complete. This means that if there is a solution breadth-first
search will find it regardless of the kind of graph. However, if the graph is infinite and
there is no solution breadth-first search will diverge.
**********70. Breadth-first search is _______ since it always returns the result with the
fewest edges between the start node and the goal node.
a. optimal
b. not optimal
c. complete
-11-

C:\Users\RAHUL\Documents\graphquestions.txt

Tuesday, January 18, 2011 7:15 PM

d. incomplete
Ans. b
In general breadth-first search is not optimal since it always returns the result with the
fewest edges between the start node and the goal node.
If the graph is a weighted graph and therefore has costs associated with each step a goal next
to the start does not have to be the cheapest goal available.
***********how inbfs*************71. _________ is a good technique for graph when there are
many possible solutions, and you only want one.
a. Breadth first search
b. Depth first search
c. Postorder
d. Inorder
Ans. b
Depth first search is good when there are many possible solutions, and you only want one (and
you don't care which one). It may be less appropriate when there is only one solution, or if
you want the shortest one.

-12-

Вам также может понравиться