Graphs
Graphs
A graph is an ordered pair G = (V, E) comprising a set V of vertices or nodes and a collection of pairs
of vertices from V , known as edges of a graph. For example, for the graph below.
V = { 1, 2, 3, 4, 5, 6 }
E = { (1, 4), (1, 6), (2, 6), (4, 5), (5, 6) }
Types of Graph
1. Undirected graph
An undirected graph (graph) is a graph in which edges have no orientation. The edge (x, y) is identical
to edge (y, x) , i.e., they are not ordered pairs. The maximum number of edges possible in an
undirected graph without a loop is n×(n-1)/2 .
2. Directed graph
A Directed graph (digraph) is a graph in which edges have orientations, i.e., The edge (x, y) is not
identical to edge (y, x) .
3. Directed Acyclic Graph (DAG)
A Directed Acyclic Graph (DAG) is a directed graph that contains no cycles.
4. Multi graph
A multigraph is an undirected graph in which multiple edges (and sometimes loops) are allowed.
Multiple edges are two or more edges that connect the same two vertices. A loop is an edge (directed
or undirected) that connects a vertex to itself; it may be permitted or not.
5. Simple graph
A simple graph is an undirected graph in which both multiple edges and loops are disallowed as
opposed to a multigraph. In a simple graph with n vertices, every vertex’s degree is at most n-1 .
An unweighted graph does not have any value (weight) associated with every edge in the graph. In
other words, an unweighted graph is a weighted graph with all edge weight as 1. Unless specified
otherwise, all graphs are assumed to be unweighted by default.
7. Complete graph
A complete graph is one in which every two vertices are adjacent: all edges that could exist are
present.
8. Connected graph
A Connected graph has a path between every pair of vertices. In other words, there are no unreachable
vertices. A disconnected graph is a graph that is not connected.
Graph Representation
1. Adjacency Matrix Representation:
An adjacency matrix is a square matrix used to represent a finite graph. The elements of the matrix
indicate whether pairs of vertices are adjacent or not in the graph.
Definition:
For a simple unweighted graph with vertex set V , the adjacency matrix is a square |V| ×
|V| matrix A such that its element:
A ij = 1 , when there is an edge from vertex i to vertex j , and
A ij = 0 , when there is no edge.
Each row in the matrix represents source vertices, and each column represents destination vertices.
The diagonal elements of the matrix are all zero since edges from a vertex to itself, i.e., loops are not
allowed in simple graphs. If the graph is undirected, the adjacency matrix will be symmetric. Also, for
a weighted graph, A ij can represent edge weights.
An adjacency matrix keeps a value (1/0/edge-weight) for every pair of vertices, whether the edge
exists or not, so it requires n 2 space. They can be efficiently used only when the graph is dense.
2. Adjacency List Representation:
An adjacency list representation for the graph associates each vertex in the graph with the collection
of its neighboring vertices or edges, i.e., every vertex stores a list of adjacent vertices. There are many
variations of adjacency list representation depending upon the implementation. This data structure
allows the storage of additional data on the vertices but is practically very efficient when the graph
contains only a few edges. i.e. the graph is sparse.