Graphs
Graphs
Graphs
GRAPHS
• Graph is a nonlinear data structure.
• The vertices are sometimes also referred to as nodes and the edges are lines or arcs that connect any two nodes in
the graph. More formally a Graph is composed of a set of vertices( V ) and a set of edges( E ). The graph is denoted by
G(E, V).
• Applications: Graphs are also used in social networks like LinkedIn, Facebook.
2/ 1 3
GRAPHS
• Representation of graphs (implementation)
1. Adjacency list:
Standard way to represent graphs
Better solution if the graph is sparse (is not dense)
2. Adjacency matrix:
Two-dimensional array
Appropriate representation if the graph is dense (having many edges)
3/ 1 3
GRAPHS
• Adjacency list
An array of lists is used. The size of the array is equal to the number of vertices.
4/ 1 3
GRAPHS
• Adjacency Matric
• Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in a graph
1 2 3 4 5 6 7
1 T T T
2 T T
3 T
4 T T T
5 T T
6
7 T
5/ 1 3
S H O R T E S T PAT H A L G O R I T H M S
• Unweighted shortest path algorithms
• We will perform a breadth-first traversal.
V3
6/ 1 3
S H O R T E S T PAT H A L G O R I T H M S
• Unweighted shortest path algorithms
V3 V1 V6
V3 V1 V6 V2 V4
7/ 1 3
S H O R T E S T PAT H A L G O R I T H M S
• Unweighted shortest path algorithms
V3 V1 V6 V2 V4 V5 V7
8/ 1 3
S H O R T E S T PAT H A L G O R I T H M S
Dijkstra’s algorithms
• We use Dijkstra’s algorithm to find the shortest path in a weighted directed graph.
• Used to solve the single-source shortest-path problem.
• It is Greedy Algorithm.
• Main idea: Calculate the distance between the nodes, and mark down every time we find a shorter path than previous
iteration.
Steps in Dijkstra’s Algorithm
• Select initial Vertex S
• Make vertex s known
• Set all distance from S to infinity
• selects a vertex, v, which has the smallest dv
among all the unknown vertices
• declares that the shortest path from s to v is known.
• set dw = dv + c(v,w) if this new value for dw would be an improvement.
Where dv is original distance & dw is updated distance
9/ 1 3
S H O R T E S T PAT H A L G O R I T H M S
Dijkstra’s algorithms
* Is known
vertices
1 0/ 1 3
S H O R T E S T PAT H A L G O R I T H M S
Dijkstra’s algorithms
* Is known
vertices
1 1/ 1 3
S H O R T E S T PAT H A L G O R I T H M S
Dijkstra’s algorithms
* Is known
vertices
1 2/ 1 3
S H O R T E S T PAT H A L G O R I T H M S
Dijkstra’s algorithms
Time complexity
O(|E| log |V| + |V| log |V|) = O(|E|
log |V|)
* Is known
vertices
1 3/ 1 3
M I N I M U M S PA N N I N G T R E E
Minimum spanning tree:
• converts graph to tree that connects vertices at lowest total cost.
• is tree because it is acyclic and open ended
• is spanning because it covers every vertex
1 5/ 1 3
M I N I M U M S PA N N I N G T R E E
Prims Algorithm: Original
Graph
Example:
1 6/ 1 3
S T R O N G LY C O N N E C T E D C O M P O N E N T S
• Strongly Connected Graph: A directed graph is strongly connected if there is a path between all pair of the vertices. (Every
vertex is reachable from every other vertex)
• Strongly Connected Components(SCC): A pair of vertices u and v are said to be strongly connected to each other if there is
a path in each direction between them.
1 7/ 1 3
S T R O N G LY C O N N E C T E D C O M P O N E N T S
How to Find SCC?
• Reverse the graph, by changing the direction of the arrows.
• Apply DFS on the reversed graph.
• Then you’ll have the strongly connected components.
Reversed
graph 1 8/ 1 3