Graph
Graph
GRAPH INTRODUCTION
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
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.
• 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.