0% found this document useful (0 votes)
14 views18 pages

Graphs

The document provides an overview of graphs as a nonlinear data structure consisting of vertices and edges, with applications in social networks. It discusses methods for graph representation, including adjacency lists and matrices, and introduces algorithms for finding the shortest path, such as Dijkstra's algorithm, and for constructing minimum spanning trees using Prim's algorithm. Additionally, it explains the concept of strongly connected components in directed graphs and the process to identify them.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views18 pages

Graphs

The document provides an overview of graphs as a nonlinear data structure consisting of vertices and edges, with applications in social networks. It discusses methods for graph representation, including adjacency lists and matrices, and introduces algorithms for finding the shortest path, such as Dijkstra's algorithm, and for constructing minimum spanning trees using Prim's algorithm. Additionally, it explains the concept of strongly connected components in directed graphs and the process to identify them.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

8.

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.

Directed (left) and undirected (right) graphs.

2/ 1 3
GRAPHS
• Representation of graphs (implementation)

We can represent graphs using one of the following methods:

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

Original Graph Corresponding minimum spanning 1 4/ 1 3


tree
M I N I M U M S PA N N I N G T R E E
Prims Algorithm:
• We use Prim’s algorithm to find a minimum spanning tree for a weighted undirected graph.
• Strategy: Always choose the vertices with closest distance to the minimum spanning tree to be included
Steps of Prims Algorithm
• 1. pick one node as the root, and add an edges
• 2. From new nodes select the one with minimum cost
• 3. Update dv (weight of the shortest edge connecting v to a known vertex )
• 4. update pv ( the last vertex to cause a change in dv)

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

You might also like