Professional Documents
Culture Documents
Graph Tree Definitions
Graph Tree Definitions
Graph Tree Definitions
non empty set of vertices {v1 ,v2 ,v3 ,…….vk} and E is a finite set of pairs
of vertices {e1,e2,……..,en} where each ej is associated with (vi,vj)
unordered, these pairs are called edges. The notations V(G) and E(G)
represent the set of vertices and edges, respectively, of graph G. We
also write G=(V,E) to represent a graph.
Undirected graph :
A graph in which the pair of vertices representing any edge is
unordered is called undirected graph. Thus the pair (vi,vj) and (vj,vi)
represent the same edge.
Examples :
Directed Graph :
A graph in which each edge is represented by a directed pair
<vi,vj>; vi is the head and vj is the tail of the edge.
Example:
Here,
d(v3)=2 and d(v2)=1 and d(v1)=3
d(v4)=2
Indegree:
In case of directed graphs, among the edges containing a vertex vi,
the number of edges pointing towards vi is called Indegree din of the
vertex vi.
Outdegree:
In case of directed graphs, among the edges containing a vertex vi,
the number of edges pointing away from vi is called outdegree dout of the
vertex vi.
here din (v1)=1,dout(v1)=2
din (v2)=1,dout(v2)=0
din (v3)=2,dout(v3)=0
din (v4)=1,dout(v4)=1
Adjacency matrix:
Let G=(V,E) be a graph with n vertices, n>=1. The adjacency
matrix of G is a two dimensional n×n array, say a, with the property that
a(i,j)=1 iff the edge (i,j) is in E(G). The element a(i,j)=0 if there is no such
edge in G.
G3
Here we can observe that G2 and G3 are transpose to one another.
If G is an undirected graph, then the adjacency matrix A of G will
be a symmetric matrix, i.e., one in whish aij=aji for every i and j. This
follows from the fact that each undirected edge (u,v) corresponds to the
two directed edges <u,v> and <v,u>.
Path :
A path is a collection of nodes beginning with vertex v i and ending
with vertex vj. There may be repetition of vertices.
Closed path:
If the beginning and ending vertices of the path are same, it is
called closed path.
Examples: 1) v1 → v2 → v4 → v1 → v3
2) v3 → v1 → v2 → v4 → v1
v1 v2
Graph G1
v3 v4
Example: For graph G1 above, v3 → v1 → v4 → v2 → v1 → v3 is a closed
path.
Open path:
If the beginning and ending vertices of the path are different, it is
called open path.
Example: For graph G1 above v3 → v1 → v4 → v2 → v1 is a open path.
Simple path:
A path in which there is no repetition of vertices is called Simple
Path.
Example: For graph G1 above v3 → v1 → v4 → v2 is a simple path.
In the graph G1, vertex v3 is not connected with any other vertices.
So, it is a disconnected Graph.
In the graph G2, all the vertices are connected. So it is a connected
Graph.
If there are ‘n’ vertices, then the minimum no. of edges required to
form a connected graph is ‘n-1’.
Tree : An undirected connected graph with n vertices and (n-1) edges
is called a tree.
(i) Rule for Children : If root is at index i then left child is at index
2i and right child is at index 2i+1.
Now let us see an algorithm for making a binary tree. In this algorithm
the terms used are,
x = value of data which is to be inserted,
P = location of parent
T[1----n] = array of size n to store data of nodes
status[1----n] = array of size n to store status (1 or 0) of node
Algorithm :
Make Tree(x, T[1----n], status[1-----n])
for i1 to n
status [i]0
T[1]x
Status[1]1
If a node does not have a left or right child then it contains NULL in that
particular field.
NULL Info Rchild
Fig.- if left child doesn’t exist
Algorithm :
Make Tree (x)
P get node()
Info(P)x
Left(P)NULL
Right(P)NULL
Set Right(P,x)
If (right(P)≠NULL) then “can’t insert”. (i.e. element exists at this position).
else
qget node()
Info(q)x
Right(P)q
Left(q)NULL
Right(q)NULL
Binary Search Tree. A binary tree in which all the elements to the
left of the node are smaller and all the elements to the right of the node
are larger is called a binary search tree(BST).