Graphs
Graphs
Introduction to Graphs
A graph contains a set of points known as nodes (or vertices) and set of links known as edges (or Arcs) which
connects the vertices.
A graph is defined as Graph is a collection of vertices and arcs which connects vertices in the graph. A graph G
is represented as G = ( V , E ), where V is set of vertices and E is set of edges.
1.Undirected Graph
2.Directed Graph
Graph Terminology
1.Vertex : An individual data element of a graph is called as Vertex. Vertex is also known as node. In above
example graph, A, B, C, D & E are known as vertices.
2.Edge : An edge is a connecting link between two vertices. Edge is also known as Arc. An edge is represented as
(starting Vertex, ending Vertex).
1.Undirected Edge - An undirected edge is a bidirectional edge. If there is an undirected edge between vertices
A and B then edge (A , B) is equal to edge (B , A).
2.Directed Edge - A directed edge is a unidirectional edge. If there is a directed edge between vertices A and
B then edge (A , B) is not equal to edge (B , A)
3.Weighted Edge - A weighted edge is an edge with cost on it.
3.Complete Graph
A graph in which any V node is adjacent to all other nodes present in the graph is known as a complete graph. An
undirected graph contains the edges that are equal to edges = n(n-1)/2 where n is the number of vertices present in
the graph. The following figure shows a complete graph.
4.Regular Graph
Regular graph is the graph in which nodes are adjacent to each other, i.e., each node is accessible from any
other node.
5.Cycle Graph
A graph having cycle is called cycle graph. In this case the first and last nodes are the same. A closed simple
path is a cycle.
6.Acyclic Graph
7. Weighted Graph
A graph is said to be weighted if there are some non negative value assigned to each edges of the graph.
The value is equal to the length between two vertices. Weighted graph is also called a network.
Outgoing Edge
Incoming Edge
Degree
Indegree
Total number of incoming edges connected to a vertex is said to be indegree of that vertex.
Outdegree
Total number of outgoing edges connected to a vertex is said to be outdegree of that vertex.
Self-loop
Simple Graph
Adjacent nodes
When there is an edge from one node to another then these nodes are called adjacent nodes.
Incidence
In an undirected graph the edge between v1 and v2 is incident on node v1 and v2.
Path
A open walk in which no vertex appears more than once is called a path.
If e1 and e2 be the two edges between the pair of vertices (v1,v3) and (v1,v2) respectively, then v3 e1 v1 e2 v2 be
its path.
Length of a path
The number of edges in a path is called the length of that path. In the following, the length of the path is 3.
Circuit
A closed walk in which no vertex (except the initial and the final vertex) appears more than once is called a
circuit.
A circuit having three vertices and three edges.
Sub Graph
A graph S is said to be a sub graph of a graph G if all the vertices and all the edges of S are in G, and each edge
of S has the same end vertices in S as in G. A subgraph of G is a graph G’ such that V(G’) V(G) and E(G’)
E(G)
Connected Graph
A graph G is said to be connected if there is at least one path between every pair of vertices in G. Otherwise,G is
disconnected.
This graph is disconnected because the vertex v1 is not connected with the other vertices of the graph.
Degree
In an undirected graph, the number of edges connected to a node is called the degree of that node or the degree of
a node is the number of edges incident on it.
In the above graph, degree of vertex v1 is 1, degree of vertex v2 is 3, degree of v3 and v4 is 2 in a
connected graph.
Indegree
The indegree of a node is the number of edges connecting to that node or in other words edges incident to it.
In the above graph,the indegree of vertices v1, v3 is 2, indegree of vertices v2, v5 is 1 and indegree of v4 is zero.
Outdegree
The outdegree of a node (or vertex) is the number of edges going outside from that node or in other words the
Graph Representations
1. Adjacency Matrix
2. Adjacency List
.Adjacency Matrix
In this representation, graph can be represented using a matrix of size total number of vertices by total number of
vertices; means if a graph with 4 vertices can be represented using a matrix of 4X4 size.
This matrix is filled with either 1 or 0. Here, 1 represents there is an edge from row vertex to column vertex and
0 represents there is no edge from row vertex to column vertex.
Adjacency Matrix : let G = (V, E) with n vertices, n 1. The adjacency matrix of G is a 2-dimensional n
n matrix, A, A(i, j) = 1 iff (vi, vj) E(G) (vi, vj for a diagraph), A(i, j) = 0 otherwise.
The adjacency matrix for an undirected graph is symmetric; the adjacency matrix for a digraph need not be
symmetric.
Merits of Adjacency Matrix:
For a digraph, the row sum is the out_degree, while the column sum is the in_degree
n1 n1
ind (vi) A[ j, i] outd(vi) A[i, j]
j 0 j 0
2
The space needed to represent a graph using adjacency matrix is n bits. To identify the edges in a
2
graph, adjacency matrices will require at least O(n ) time.
2. Adjacency List
In this representation, every vertex of graph contains list of its adjacent vertices. The n rows of the adjacency
matrix are represented as n chains. The nodes in chain I represent the vertices that are adjacent to vertex i.
So that we can access the adjacency list for any vertex in O(1) time. Adjlist[i] is a pointer to to first node in
the adjacency list for vertex i
Another type of representation is given below.
example: consider the following directed graph representation implemented using linked list
4. Weighted edges
In many applications the edges of a graph have weights assigned to them. These weights may represent the
distance from one vertex to another or the cost of going from one; vertex to an adjacent vertex In these
applications the adjacency matrix entries A [i][j] would keep this information too. When adjacency lists are used
the weight information may be kept in the list’nodes by including an additional field weight. A graph with
weighted edges is called a network.
ELEMENTARY GRAPH OPERATIONS(Graph Traversals)
Given a graph G = (V E) and a vertex v in V(G) we wish to visit all vertices in G that are reachable from v (i.e.,
all vertices that are connected to v). We shall look at two ways of doing this: depth-first search and breadth-
first search. Although these methods work on both directed and undirected graphs the following discussion
assumes that the graphs are undirected.
Depth-First Search
We begin by visiting the start vertex v. Next an unvisited vertex w adjacent to v is selected, and a depth-first
search from w is initiated. When a vertex u is reached such that all its adjacent vertices have been visited, we back
up to the last vertex visited that has an unvisited vertex w adjacent to it and initiate a depth-first search from w.
The search terminates when no unvisited vertex can be reached from any of the visited vertices.
DFS traversal of a graph, produces a spanning tree as final result. Spanning Tree is a graph without any loops. We
use Stack data structure with maximum size of total number of vertices in the graph to implement DFS
traversal of a graph.
Step 2: Select any vertex as starting point for traversal. Visit that vertex and push it on to the Stack.
Step 3: Visit any one of the adjacent vertex of the verex which is at top of the stack which is not visited and push
it on to the stack.
Step 4: Repeat step 3 until there are no new vertex to be visit from the vertex on top of the stack.
Step 5: When there is no new vertex to be visit then use back tracking and pop one vertex from the stack.
Step 7: When stack becomes Empty, then produce final spanning tree by removing unused edges from the graph
Breadth-First Search
In a breadth-first search, we begin by visiting the start vertex v. Next all unvisited vertices adjacent to v are
visited. Unvisited vertices adjacent to these newly visited vertices are then visited and so on.
Steps:
BFS traversal of a graph, produces a spanning tree as final result. Spanning Tree is a graph without any loops. We
use Queue data structure with maximum size of total number of vertices in the graph to implement BFS traversal
of a graph.