Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views6 pages

Graph Revision Notes

The document provides notes on graph algorithms including cycle detection in undirected and directed graphs using DFS and BFS, as well as topological sorting methods. It discusses Dijkstra's and Bellman-Ford algorithms for finding shortest paths, highlighting their time and space complexities. Key points include the importance of indegree in cycle detection and the iteration requirements for the Bellman-Ford algorithm to ensure all shortest paths are found.

Uploaded by

Piyush Agarwal
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)
6 views6 pages

Graph Revision Notes

The document provides notes on graph algorithms including cycle detection in undirected and directed graphs using DFS and BFS, as well as topological sorting methods. It discusses Dijkstra's and Bellman-Ford algorithms for finding shortest paths, highlighting their time and space complexities. Key points include the importance of indegree in cycle detection and the iteration requirements for the Bellman-Ford algorithm to ensure all shortest paths are found.

Uploaded by

Piyush Agarwal
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/ 6

Graph Revision Notes:

Cycle in undirected graph dfs

Time Complexity: O(N + 2E) + O(N), Where N = Nodes, 2E is for total degrees as we traverse all
adjacent nodes. In the case of connected components of a graph, it will take another O(N) time
Space Complexity: O(N) + O(N) ~ O(N), Space for recursive stack space and visited array.

Cycle in undirected graph BFS

Topological Sorting
Cycle In directed graph (BFS)
Linear ordering of vertices such that if there is a edge between u and v then u must appear
before v, Graph must be directed acyclic (DAG)

Code(DFS)​ ​ ​ ​ ​ ​ ​ ​ Algo(DFS)

1.We must traverse all components of the graph.
2. sure to carry a visited array(all elements are
initialized to 0) and a stack data structure, where
we are going to store the nodes after completing
the DFS call.
3.In the DFS call, first, the current node is marked
as visited. Then DFS call is made for all its adjacent
nodes.
4.After visiting all its adjacent nodes, DFS will
backtrack to the previous node and meanwhile, the
current node is pushed into the stack.
5.Finally, we will get the stack containing one of the
topological sortings of the graph

​ ​ Intution
Since we are inserting the nodes into the stack
after the completion of the traversal, we are making
sure, there will be no one who appears afterward
but may come before in the ordering as everyone
during the traversal would have been inserted into
the stack.

Khans Algo​ for toposort (BFS)​ ​ ​ ​ ​ Algo


​ ​ ​ ​ ​

Calculate indegree of each node


Start with node with indgreee o
Zero
While visiting its adjacent node reduce their
indrgee and when indfree of any node is
zero push it into queue

For detecting cycle using bfs just check if


toposort vector has sme number of
elements as no of vertices in graph

Imp Note:- A graph with no node as


indegree as 0 must contains cycle but
vice-versa is not true
Shortest path undirected graph with unit weight​ ​ Shortest path directed graph with weights
Dijktra’s Algorithm (dont wrk for graph with negative edge weights)​
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ TC-O(E)log(V) SC-O(E+V)

Bellman Ford Algorithm (work with directed graph only, used for -ve weights) In order to apply this
algorithm to an undirected graph, we just need to convert the undirected edges into directed edges
it is able to detect if the graph contains a negative cycle

3. Why V−1V-1V−1 Iterations?

👉 A shortest path in a graph


with VVV vertices can have at

👉
most V−1V-1V−1 edges.​
Each iteration propagates
shortest distances one step

👉
further.​
By the V−1V-1V−1th
iteration, every shortest path
must be found.

🔹 Example:
●​ Suppose we have a
graph with 4 vertices
(A, B, C, D).
●​ The longest shortest
path can have at most
3 edges.
●​ After 3 iterations, all
shortest paths must
be finalized.

Time Complexity: O(V*E), where V = no. of vertices and E = no. of Edges.


Space Complexity: O(V)

You might also like