Trees and Graphs3
Trees and Graphs3
Graphs
Graph is a non linear data structure, it 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 follows...
Example
The following is a graph with 5 vertices and 6
edges.
This graph G can be defined as
G=(V,E)
Where V = {A,B,C,D,E}
and E = {(A,B),(A,C)(A,D),(B,D),(C,D),(B,E),(E,D)}.
Graph Terminology
We use the following terms in graph data
structure...
Vertex
A 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.
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),
(A,C), (A,D), (B,D), (B,E), (C,D), (D,E)).
Undirected Graph
A graph with only undirected edges is said to be
undirected graph.
Directed Graph
A graph with only directed edges is said to be
directed graph.
Mixed Graph
A graph with undirected and directed edges is
said to be mixed graph.
Origin
If an edge is directed, its first endpoint is said to
be origin of it.
Destination
If an edge is directed, its first endpoint is said to
be origin of it and the other endpoint is said to be
the destination of the edge.
Adjacent
If there is an edge between vertices A and B then
both A and B are said to be adjacent. In other
words, Two vertices A and B are said to be
adjacent if there is an edge whose end vertices
are A and B.
Incident
An edge is said to be incident on a vertex if the
vertex is one of the endpoints of that edge.
Outgoing Edge
A directed edge is said to be outgoing edge on its
origin vertex.
Incoming Edge
A directed edge is said to be incoming edge on its
destination vertex.
Degree
Total number of edges connected to a vertex is
said to be degree of that vertex.
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
An edge (undirected or directed) is a self-loop if
its two endpoints coincide.
Simple Graph
A graph is said to be simple if there are no
parallel and self-loop edges.
Path
A path is a sequence of alternating vertices and
edges that starts at a vertex and ends at a 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, graph can be represented
using a matrix of size total number of vertices by
total number of vertices. That means if a graph
with 4 vertices can be represented using a matrix
of 4X4 class. In this matrix, rows and columns
both represents vertices. 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.
Incidence Matrix
In this representation, graph can be represented
using a matrix of size total number of vertices by
total number of edges. That means if a graph
with 4 vertices and 6 edges can be represented
using a matrix of 4X6 class. In this matrix, rows
represents vertices and columns represents
edges. This matrix is filled with either 0 or 1 or
-1. Here, 0 represents row edge is not connected
to column vertex, 1 represents row edge is
connected as outgoing edge to column vertex and
-1 represents row edge is connected as incoming
edge to column vertex.
Adjacency List
In this representation, every vertex of graph
contains list of its adjacent vertices.
Graph Traversals –
DFS(Depth first
Search)
Graph traversal is technique used for searching a
vertex in a graph. The graph traversal is also used
to decide the order of vertices to be visit in the
search process. A graph traversal finds the egdes
to be used in the search process without creating
loops that means using graph traversal we visit all
vertices of graph without getting into looping
path.
Example
Graph Traversals -
BFS
BFS (Breadth First Search)
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.
We use the following steps to implement BFS
traversal...
Step 1: Define a Queue of size total number
of vertices in the graph.
Step 2: Select any vertex as starting
point for traversal. Visit that vertex and
insert it into the Queue.
Step 3: Visit all the adjacent vertices of the
vertex which is at front of the Queue which is
not visited and insert them into the Queue.
Step 4: When there is no new vertex to be
visit from the vertex at front of the Queue
then delete that vertex from the Queue.
Step 5: Repeat step 3 and 4 until queue
becomes empty.
Step 6: When queue becomes Empty, then
produce final spanning tree by removing
unused edges from the graph
Example