A graph is a non-linear data structure that consists of a set of non-empty Vertices with a set of Edges.Each edge joins two different Vertices.
- Undirected Graph
- Directed Graph
If an edge between any two nodes is not directly oriented, then it is undirected graph
(A,B) & (B,A) represents the same edge
If an edge between any two nodes is *directly oriented, then it is directed graph
(A,B) Shows the edge between A and B
Here A is called the Tail node and B is called the Head node
- A graph may not have an edge from a vertex V back to itself. If it has an eged to itself, then it is called Self edges.
- A graph may not have multiple occurrence of the same edges (multigraph)
A path is a sequence of distinct Vertices each adjacent ot the next.
A cycle is a simple path in which first and last vertices are same.
In a digraph. a cycle is referred as Directed Cycle.
Note : The maximum number of edges in a graph with n vertices is n(n+1)/2
An n-vertex, undirected graph with exactly n(n+1)/2 edges is said to be a complete graph.
A subgraph of G is a graph G' such that *V(G') is a subset of V(G)
If there exists a path from any vertex to any other vertex, then that graph is called connected graph.
If for every pair of distinct vertices there is a directed path from every vertex to every other vertices, then that graph is called Strongly connected graph.
If any vertex dosen't have a directed path to any other vertices, then the graph is called Weakly connected Graph.
Undirected graph only have one degree, ie. the The number of edges connected directly to a node
Directed Graphs have two types of degrees, namely
- Indegree - The number of edges entering the node.
- Outdegree - The number of edges leaving the node.
Node | Indegree | Outdegree |
---|---|---|
A | 1 | 1 |
B | 1 | 1 |
A vertex whose indegree is zero is referred as source vertex.
A vertex whose outdegree is zero is referred as sink vertex.
If a graph has onlu one vertex in it, it is a isolated graph vertex.
If every edge in the graph is assigned some weight (or) value, then the graph is called Weighted Graph
A vertex whose indegree is 1 and outdegree is 0 is referred to as Pendant Vertex.
A graph containing m vertices and n edges can be represented by a matrix with m rows and n columns. The matrix is formed by storing 1 in the ith row and jth column corresponding to the matrix, if there exists a ith vertex connected to one end of the jth edge and a 0 if there is no ith vertex connected to any end of the jth edge of the graph, such a matrix is referred to as incidence matrix.
Consider the given weighted graph
incidence_matrix[i][j] = 1, if there is an edge
0, otherwise
The Incidence Matrix for this graph is given by:
E1 | E2 | E3 | |
---|---|---|---|
V1 | 1 | 0 | 0 |
V2 | 0 | 1 | 0 |
V3 | 0 | 0 | 1 |
V4 | 1 | 1 | 1 |
A graph containing n vertices can be represented by a matrix with n rows and n columns if there exists an edge between ith and jth vertex of the graph, then 1 is stored in the ith row and jth column of the matrix, otherwise 0 is stored.
The Adjacency matrix for the graph is given by:
A | B | C | D | |
---|---|---|---|---|
A | 0 | 1 | 0 | 0 |
B | 0 | 0 | 1 | 0 |
C | 1 | 0 | 0 | 0 |
D | 1 | 1 | 1 | 0 |
A graph containing m vertices and n edges can be represented using a linked list is referred to as adjacency list.
V1 | V2 |
---|