Graph
Directed vs UnDirected Graph
Directed Graph
Degree = no. edges from a graph
Sum of degrees = 2 x | no of edges |
max number of edge = [ v * (v-1) ]/2
Undirected Graph
Degree = in-degree + out-degree
Sum of in-degree = | no of edges |
Sum of out-degree = | no of edges |
max number of edge = v * (v-1)
Concepts on graph
Walk (sometimes path) : from going one edge to another via connected edges
v1 -> v2 -> v4 -> v2
if no edge exists between 2 vertices, you cannot walk directly
Path (simple path) : a walk without repeated vertices allowed
Degree : all edges to vertices
in-degree : directed graph incoming edges
out-degree : directed graph outgoing edges
Cyclic : if the walk starts and ends on the same vertices
they can directed and undirected
Acyclic :
Undirected acyclic graph (UDAG)
Directed acyclic graph (DAG)
Weighted graph : edges with some weight assigned to it
unweighted graph : simple edges
Adjacency Matrix vs List
Adjacency Matrix

If the diagonal of matrix is 0 and upper and lower triangle are symmetric, then it is a undirected graph
size of matrix = |v|x|v|
If graph is not symmetric , then it is a directed graph

space required : |v|x|v|
check if u & v are adj : O(1)
find all vertices : O(v) : simple matrix row traversal
adding/removing edges : O(1)
Adjacency Matrix List


space required : O(V+E)
undirected : V+2E
directed : V+E
check if there is a edge from u to v : O(v)
find all adjacency of u : O(degree(u))
Find degree of u : O(1)
Add an edge : O(1)
remove and edge : O(v)
Applications of DFS vs BFS
Applications of Breadth First Search
Shortest Path in unweighted graph
Crawler in Search Engine
Peer to Peer network
Social Networking Search
In garbage collection (Cheney's Algorithm)
Cycle Detection
Ford Fulkerson Algorithm
Broadcasting
Applications of Depth First Search
Cycle Detection
Topological Sorting
Solving Maze and Puzzle Problems
Path Finding
Adjacency List implementation in Java
Breadth First Search Graph Traversal
How it works
create a queue to maintain elements for breadth first search
to avoid repeating the visited nodes in cyclic graph, create a boolean array of size |v| to mark array[i] as true/false indicating visited status
CODE FOR BFS disconnected graph is same
Disconnected Graph: How it works
same as BFS, the algorithm is called for every node assuming it is the source vertice of the disconnected graph.
we keep track of visited vertices in an boolean array just like BFS but this visited array is passed from source caller to BFS algorithm (in Java : array is pass by reference)
this visited array will avoid calling BFS for nodes of island as they would have been visited
To count the number of island in graph
keep track of island source with counter
every islands nodes will be ignored as they will be marked as visited by BFS
Depth First Search
How it works
Depth first search is achieved with recursion just like tree
To avoid cyclic loops, a boolean array containing visited nodes is maintain and passed in the recursion
Disconnected Graph: How it works
same as BFS, the algorithm is called for every node assuming it is the source vertice of the disconnected graph.
we keep track of visited vertices in an boolean array just like DFS but this visited array is present in recursion call from source caller to DFS algorithm (in Java : array is pass by reference)
this visited array will avoid calling DFS for nodes of island as they would have been visited
------------
BFS : Shortest Path in an Unweighted Graph
DFS : Detect Cycle in Undirected Graph
DFS : Part 1 : Detect Cycle in a Directed Graph
Topological Sorting (Kahn's BFS Based Algortihm)
Part 2 : Detect Cycle in a Directed Graph ( using kahn's Algorithm )
Topological Sorting (DFS Based Algorithm)
Shortest Path in Directed Acyclic Graph
Prim's Algorithm / Minimum Spanning Tree
Dijkstra's Shortest Path Algorithm
Kosaraju's Algorithm Part 1
Bellman Ford Shortest Path Algorithm
Articulation Point
Bridges in Graph
Tarjans Algorithm
Kruskal's Algorithm
Prim's Algorithm
Dijkstra's Algorithm Java
Kosaraju's Algorithm
Bellman Ford Shortest Path Algorithm
Last updated