0% found this document useful (0 votes)
12 views

Graph

Uploaded by

nvkeerthuvcet
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Graph

Uploaded by

nvkeerthuvcet
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

GRAPHS

GRAPH INTRODUCTION

• Graph is a non-linear data structure.


• A Graph is a collection of two sets V and E where V is finite non-empty
set of vertices and E is finite non empty set of edges.
• Vertices are nothing but nodes in the graph. Two adjacent vertices are
joined by edges.
• A graph is denoted by G={V,E} .
• A closed structure of nodes and edges will always be a graph. But tree
cannot have closed structure.
Complete Graph
 A complete graph is a graph that has the maximum number
of edges
– for undirected graph with n vertices, the maximum number of
edges is n(n-1)/2
– for directed graph with n vertices, the maximum number of edges
is n(n-1)
– example: G1 is a complete graph
Examples for Graph
0 0 0

1 2 1 2
1
3
3 4 5 6
G1 2
G2
complete graph incomplete graph G3
V(G1)={0,1,2,3} E(G1)={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)}
V(G2)={0,1,2,3,4,5,6} E(G2)={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)}
V(G3)={0,1,2} E(G3)={<0,1>,<1,0>,<1,2>}
complete undirected graph: n(n-1)/2 edges
complete directed graph: n(n-1) edges
Example:
• The following is a simple graph 5 vertices and 7 edges.
• Where V={A,B,C,D,E} and E={(A,B),(A,C),(A,D),(B,D),(C,D),(B,D),(E,D)}
Graph terminologies:
1.Vertex
• 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 (startingVertex, ending
Vertex). 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)).
3.Adjacent nodes:
• nodes which are connected by an edge is called adjacent nodes. In the above
example A and B, D and E , B and D are adjacent nodes.
3. Degree of that node:
• Represents the number of edges connected to that node. In the above
example degree of A is 3 and E is 2.
4. Size of the graph:
• Total number of edges in the graph. In the example size of the graph is 7.
5. Path:
• Sequence of vertices from the source node to the destination. Path from A
to E is A->B->E or A->D->E.
6. Component:
• The maximal connected subgraph of a graph is called component of a graph.
• A directed graph is strongly connected if there is a path between all pairs of
vertices.
6. Cycle:
• A closed traverse through the graph with repeated vertices mostly having
the starting and ending vertex is called a cycle.
7. Indegree and Outdegree:
• Degree is the number of edges associated with the vertex.
• Indegree is the number of edges that incident to that vertex. Outdegree is
the number of edges that are going away from the vertex.
• Indegree of 2 is 3, outdegree of 2 is 2.
8. Self loop:
• It is an edge that connects the same vertex to itself.
• Eg. Node-3.
Figure 6.4: subgraphs of G1 and G3 (p.261)

0
0 0 1 2 0
1 2
1 2 3 1 2
3
3
G1 (i) (ii) (iii) (iv)
(a) Some of the subgraph of G1

0 0 0 0
0

1 1 1 1

(i) (ii) (iii) 2 (iv) 2


2 (b) Some of the subgraph of G3

G3
Types of the graph:
•Directed graph
•Undirected graph
•Weighted graph
•Unweighted graph
•Cyclic Graph
•Acyclic graph
1. Directed Graph:
• Traversal can be in one direction only. In the example (1,3)!=(3,1)
2. Undirected Graph:
• Traversal can be in both the direction. In the example (1,2)=(2,1)
• It is a bi-directional graph.
3. Weighted graph:
• If the edge contains any weight or cost then it is called as weighted
graph.
4. Unweighted graph:
• If the edges contains no weight then it is called as unweighted graph.
5.Cyclic graph:
• The graph forms at least a cycle.
• If the starting and ending node in the path sequence are same then it
is called cyclic graph.
• (i.e) graph contains loops called cyclic graph. Eg. B->C->E->D->B
6. Acyclic graph:
• Graph contains no cycle/loop is called acyclic graph.

Figure: Cyclic graph


Representation of Graph:
• Graph can be represented in two ways.
 Using multi dimensional array.
 Using List.
1. Representation of graph using multi dimensional array/ matrix:
• Consider the undirected graph

• Consider a directed graph,


2. Graph representation using List:
• Linked list representation of undirected graph.
Linked list representation of directed graph:
Applications of Graph:

• In computer networking such as local area network(LAN), wide area


network(WAN), internetworking.
• In telephone cabling graph theory is effectively used.
• In job scheduling algorithms the graphs are used.
Graph traversals:
• Graph traversal means visiting all nodes.
• Here in graph we are not having root node. We can start the traversal
from any node.
• We can traverse all the nodes in the graph without any repetition.
Every node of the graph should be visited only once.
• We had two types of traversals or searches.
Depth first search(DFS)  will use STACK data structure
Breadth first search(BFS) will use QUEUE data structure
DFS (Depth First Search)

• 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.
• We use the following steps to implement DFS traversal...
• Step 1 - Define a Stack of size total number of vertices in the 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 non-visited adjacent vertices of a vertex which is at the top of stack and push
it on to the stack.
• 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 6 - Repeat steps 3, 4 and 5 until stack becomes Empty.
• Step 7 - When stack becomes Empty, then produce final spanning tree by removing unused edges from the
graph
• Back tracking is coming back to the vertex from which we reached the current vertex.
DFS PROGRAM
BFS (Breadth First Search)
• 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.

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 non-visited adjacent vertices of the vertex which is at front of the
Queue and insert them into the Queue.
• Step 4 - When there is no new vertex to be visited from the vertex which is at front of the
Queue then delete that vertex.
• Step 5 - Repeat steps 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
BFS PROGRAM:

You might also like