Data Sructures Graphs
Data Sructures Graphs
Unit 4: Graphs
What is Graph?
1. Vertices
2. Edges
Graph is a set of vertices (V) and set of edges (E).
V is a finite number of vertices also called as nodes.
E is a set of ordered pair of vertices representing edges.
For example, in Facebook, each person is represented with a vertex or a node.
Each node is a structure and contains the information like user id, user name,
gender etc.
The above figures represent the graphs. The set representation for each of
these graphs are as follows:
Graph 1:
V = {A, B, C, D, E, F}
E = {(A, B), (A, C), (B, C), (B, D), (D, E), (D, F), (E, F)}
Graph 2:
V = {A, B, C, D, E, F}
E = {(A, B), (A, C), (B, D), (C, E), (C, F)}
Graph 3:
V = {A, B, C}
E = {(A, B), (A, C), (C, B)}
Directed Graph
Undirected Graph
Representation of Graphs
Adjacency Matrix
Adjacency matrix is a way to represent a graph.
It shows which nodes are adjacent to one another.
Graph is represented using a square matrix.
Graph can be divided into two categories:
a. Sparse Graph
b. Dense Graph
a. Sparse graph contains less number of edges.
b. Dense graph contains number of edges as compared to sparse graph.
Adjacency matrix is best for dense graph, but for sparse graph, it is not
required.
Adjacency matrix is good solution for dense graph which implies having
constant number of vertices.
The above graph represents undirected graph with the adjacency matrix
representation. It shows adjacency matrix of undirected graph is symmetric. If
there is an edge (2, 4), there is also an edge (4, 2).
The above graph represents directed graph with the adjacency matrix
representation. It shows adjacency matrix of directed graph which is never
symmetric. If there is an edge (2, 4), there is not an edge (4, 2). It indicates
direct edge from vertex i to vertex j.
Adjacency List
Adjacency list is another representation of graphs.
It is a collection of unordered list, used to represent a finite graphs.
Each list describes the set of neighbors of a vertex in the graph.
Adjacency list requires less amount of memory.
For every vertex, adjacency list stores a list of vertices, which are adjacent to
the current one.
In adjacency list, an array of linked list is used. Size of the array is equal to the
number of vertices.
Graph Traversal
************************************************************************************
Telegram Channel:
https://t.me/scholard_techguide
Telegram Channel:
https://t.me/Scholard_careers
************************************************************************************