0% found this document useful (0 votes)
4 views13 pages

L26 Graph4

The document outlines common graph-related interview problems, including cycle detection and checking if a graph is a tree or strongly connected. It discusses algorithms such as DFS, BFS, Kahn's algorithm for topological sorting, and the Bellman-Ford algorithm for handling negative weight edges. Additionally, it highlights the challenges posed by negative cycles in graph algorithms.

Uploaded by

himoce6228
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views13 pages

L26 Graph4

The document outlines common graph-related interview problems, including cycle detection and checking if a graph is a tree or strongly connected. It discusses algorithms such as DFS, BFS, Kahn's algorithm for topological sorting, and the Bellman-Ford algorithm for handling negative weight edges. Additionally, it highlights the challenges posed by negative cycles in graph algorithms.

Uploaded by

himoce6228
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

L28: Graph 4:

Common Interview Problems


Common Interview Questions
• Find whether an undirected graph has a cycle or not?
• Find whether a Directed Graph has a cycle or not?
• Check if a given graph is tree or not?
• Find out whether an undirected graph is strongly
connected or not?
• Find out whether a Directed Graph is strongly connected
or not ?

2
3
Graph is tree or not
• Undirected graph is a tree if it is connected and has no
cycle
• Soln A: DFS based soln of undirected graph should work
• Traverse using DFS , and if all nodes get covered, done
• Soln B: BFS, Traverse using BFS , and if all nodes get
covered, done

4
Directed Graph is tree or not
• Following all steps/conditions needed:
• Tree contains a single vertex, named as root (vertex
having no incoming edge)
• Starting from root, graph must be connected
• Any thing else?
Yes, each node except root must have a single parent.

• Will in-degree based checks be sufficient?

5
Graph is strongly connected or not
Undirected Graph:
• Easy: If traversal (DFS/BFS) covers all vertices in single
call i.e. if dfs_visit(0) covers all vertices, it is strongly
connected.

6
Graph is strongly connected or not
Directed Graph:
• Use SCC algo and if its solution contains only one
component, graph is strongly connected. If more than one
component comes, it is not strongly connected.

7
Kahn’s Algo for Topological Sorting
• In-degree and out-degree based
• Main Logic: keep all vertices with zero in-degree in a
queue.
• Take a vertex with zero in-degree and reduce the in-
degree of all its adjacents (effectively equal to removing
this vertex with all of its outgoing edges ).
• If there was no cycle, atleast one more vertex will
become with zero in-degree. So above step continues.
• If cycle existed, all vertices in the cycle will still have
their in-coming as well as outgoing edges intact.

8
Kahn’s Algo for Topological Sorting
• Step-1: Compute in-degree for each of the vertex present
in the DAG
• Step-2: Pick all the vertices with in-degree as 0 and add
them into a queue
• Step-3:
– Remove a vertex from the queue and increment count of
visited nodes by 1.
– Decrease in-degree by 1 for all its neighboring nodes.
– If in-degree of a neighboring nodes is reduced to zero, then
add it to the queue.
• Step 4: Repeat Step 3 until the queue is empty.
• Step 5: If count of visited nodes is not equal to the
number of nodes in the graph topological sort is not
possible. 9
10
Negative Weight Edges
B 6
5
-4
C
A D
3
• Negative weight edges may create problem
• For ex: Dijkastra’s algo : from vertex A, smallest weight
edge first time will be D (greedy choice, to be optimal)
• Later path A-B-D makes it +1, shortest distance for A-D
does not remain optimal.
• Negative Cycle (cycle with overall weight of the cycle
being negative) will further create problems, as each
iteration of the cycle will start reducing the total weight.
11
Negative Weight Edges
• Bellman Ford Algo can handle –ive weights, not negative
cycles)
• Greedy choice not possible, hence algo will take more
time
• A path is assumed as shortest, only after max-possible
length (i.e. n-1 edges) gets considered. So n-1 iterations
are done for each path effectively.

12
Bellman ford Algo
initialize_single_source //distance of all vertices from s as +∞
for (i=1;i<=n-1;i++)
for each edge (u,v)
relax (u,v,w)

for each edge (u,v)


if dist[v] > d[u] +wt[u,v] //if any dist can still reduce
return FALSE //indicates –ive cycle
• Ɵ(VE)

13

You might also like