Introduction To Graphs-Representation-Traversals
Introduction To Graphs-Representation-Traversals
Graph is a non-linear data structure. It contains a set of points known as nodes (or vertices) and a
set of links known as edges (or Arcs). Here edges are used to connect the vertices. A graph is
defined as follows...
Graph is a collection of vertices and arcs in which vertices are connected with arcs
Graph is a collection of nodes and edges in which nodes are connected with edges
edges.
Example
Graph Terminology
Vertex
Individual data element of a graph is called as Vertex. Vertex is also known as node. In above
Edge
An edge is a connecting link between two vertices. Edge is also known as Arc. An edge is
represented as (startingVertex, endingVertex). For example, in above graph the link between
vertices A and B is represented as (A,B). In above example graph, there are 7 edges (i.e., (A,B),
2. Directed Edge - A directed egde is a unidirectional edge. If there is directed edge between
Undirected Graph
Directed Graph
Mixed Graph
A graph with both undirected and directed edges is said to be mixed graph.
The two vertices joined by edge are called end vertices (or endpoints) of that edge.
Origin
Destination
If a edge is directed, its first endpoint is said to be the origin of it and the other endpoint is said to be
Adjacent
If there is an edge between vertices A and B then both A and B are said to be adjacent. In other
words, vertices A and B are said to be adjacent if there is an edge between them.
Incident
Edge is said to be incident on a vertex if the vertex is one of the endpoints of that edge.
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.
Parallel edges or Multiple edges
If there are two undirected edges with same end vertices and two directed edges with same origin
and destination, such edges are called parallel edges or multiple edges.
Self-loop
Edge (undirected or directed) is a self-loop if its two endpoints coincide with each other.
Simple Graph
Path
A path is a sequence of alternate vertices and edges that starts at a vertex and ends at other vertex
such that each edge is incident to its predecessor and successor vertex.
Graph Representations
Graph data structure is represented using following representations...
1. Adjacency Matrix
2. Incidence Matrix
3. Adjacency List
Adjacency Matrix
In this representation, the graph is represented using a matrix of size total
number of vertices by a total number of vertices. That means a graph with 4
vertices is represented using a matrix of size 4X4. In this matrix, both rows and
columns represent vertices. This matrix is filled with either 1 or 0. Here, 1
represents that there is an edge from row vertex to column vertex and 0
represents that there is no edge from row vertex to column vertex.
Incidence Matrix
In this representation, the graph is represented using a matrix of size total
number of vertices by a total number of edges. That means graph with 4 vertices
and 6 edges is represented using a matrix of size 4X6. In this matrix, rows
represent vertices and columns represents edges. This matrix is filled with 0 or 1
or -1. Here, 0 represents that the row edge is not connected to column vertex, 1
represents that the row edge is connected as the outgoing edge to column vertex
and -1 represents that the row edge is connected as the incoming edge to
column vertex.
Adjacency List
In this representation, every vertex of a graph contains list of its adjacent
vertices.
decide the order of vertices is visited in the search process. A graph traversal finds the edges to be used in the
search process without creating loops. That means using graph traversal we visit all the vertices of the graph
There are two graph traversal techniques and they are as follows...
DFS traversal of a graph produces a spanning tree as final result. Spanning Tree is a graph without loops.
We use Stack data structure with maximum size of total number of vertices in the graph to implement DFS
traversal.
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 non-visited adjacent vertices of a vertex which is at the top of stack and
Step 4 - Repeat step 3 until there is no new vertex to be visited from the vertex which is at the top of
the stack.
Step 5 - When there is no new vertex to 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
Back tracking is coming back to the vertex from which we reached the current vertex.
Example
Graph Traversal - BFS
Graph traversal is a technique used for searching a vertex in a graph. The graph traversal is also used to
decide the order of vertices is visited in the search process. A graph traversal finds the edges to be used in the
search process without creating loops. That means using graph traversal we visit all the vertices of the graph
There are two graph traversal techniques and they are as follows...
BFS traversal of a graph produces a spanning tree as final result. Spanning Tree is a graph without loops.
We use Queue data structure with maximum size of total number of vertices in the graph to implement BFS
traversal.
Step 2 - Select any vertex as starting point for traversal. Visit that vertex and insert it into the Queue.
Step 3 - Visit all the non-visited adjacent vertices of the vertex which is at front of the Queue and
Step 4 - When there is no new vertex to be visited from the vertex which is at front of the Queue then
Step 6 - When queue becomes empty, then produce final spanning tree by removing unused edges