Concept and Terminologies
•Definition- A Graph is a collection of two sets V and E where
V is a finite non-empty set of vertices and E is a finite non-
empty set of edges.
• Vertices are nothing but the nodes in the graph.
• Two adjacent vertices are joined by edges.
• Any graph is denoted as G={V,E}.
4.
Graph as anADT
AbstractDataType Graph
{
Instances- Graph is a collection of vertices and edges.
Operations-
1. tot_vertices()- total number of vertices.
2. tot_edges()- total number of edges.
3. Is_edge(V1,V2)- edge between V1 and V2.
4. In_degree()- total number of edges that are incident to
the
vertex.
5. Out_degree()- out degree of the vertex.
6. display()- displays graph in DFS or BFS manner.
5.
Types of Graph
1.Directed Graph-
• A graph containing ordered pair of vertices is called a
directed graph.
• If an edge is represented using a pair of vertices(V1,V2)
then the edge is said to directed from V1 to V2.
• V1 is called predecessor of V2. and V2 is successor of
V1.
• In directed graph, the pairs(V1,V2) and (V2,V1)
represent two different edges of a graph.
Types of Graph
2.Undirected Graph-
• A graph containing pair of vertices is called an
undirected graph.
• In undirected graph, the pairs(V1,V2) and (V2,V1)
represent same edges of a graph.
Properties of Graph
1.A Complete Graph
• An undirected graph, in which every vertex has an edge to
all other vertices is called a complete graph.
• A complete graph with N vertices has N(N-1)/2 edges.
10.
Weighted Graph
• Aweighted graph is a graph in which edges are assigned
some value.
11.
Connected Graph
• Anundirected graph is said to be connected if for every pair
of distinct vertices Vi and Vj in V(G) there is an edge Vi to
Vj in G.
12.
• Subgraph-
A subgraphof G1 is a graph G, such that V(G) is a
subset of V(G1) and E(G) is a subset of E(G1).
• Path-
A pathdenoted using sequence of vertices and there
exists an edge from one vertex to the next vertex .
15.
• Cycle-
A closedwalk through the graph with repeated
vertices, mostly having the same starting and ending vertex is
called a cycle.
16.
• Degree ofVertex-
The degree of vertex is number of edges associated
with the vertex.
In-degree of Vertex-
The number of edges that incident to that vertex.
Out-degree of Vertex-
Total number of edges that are going away from the
vertex.
17.
• Self Loop-
Selfloop is an edge that connects the same vertex to
itself.
• Isolated Vertex-
In a graph the vertex with degree 0 is called
isolated vertex
18.
• Adjacent Vertices-
Ina graph, two vertices are adjacent if they are
joined by an edge.
Here, vertices A and B, A and D,
B and C, D and C are adjacent
vertices.
19.
Representation of Graphusing Adjacency
matrix and Adjacency list
1. Adjacency matrix-
In this representation, matrix or 2 dimensional array is
used to represent the graph.
2. Adjacency List-
In this representation, a linked list is used to
represent a graph. There are two methods of representing
graph using adjacency list.
20.
Breadth First SearchTraversal
• In BFS, we start from some vertex and find all the adjacent
vertices of it. This process will be repeated for all the vertices
so that the vertices lying on same breadth get printed.
• For avoiding repetition of vertices, we maintain array of
visited nodes.
• A queue data structure is used to store adjacent vertices.
21.
Breadth First SearchTraversal
• Algorithm:
1. Create a graph. Depending on the type of graph i.e. directed
or undirected set the value of the flag as either 0 or 1 resp.
2. Read the vertex from which you want to traverse the graph
say Vi.
3. Initialize the visited array to 1 at the index of Vi.
4. Insert the visited vertex Vi in the queue.
5. Visit the vertex which is at the front of the queue. Delete it
from the queue and place its adjacent nodes in the queue.
6. Repeat the step 5, till the queue is not empty.
7. Stop.
22.
Depth First SearchTraversal
• In Depth First search traversal we start from one vertex and
traverse the path as deeply as we can go.
• When there is no vertex further we traverse back and search
for unvisited vertex.
• An array is maintained for storing the visited vertex.
23.
Spanning Tree
• Aspanning tree of a graph G=(V,E) is connected subgraph of
G having all vertices of G and no cycles in it.
• If the graph G is not connected then there is no spanning tree
of G.
• Minimal Spanning Tree-
the cost of a graph is the sum of the costs of
the edges in the weighted graph.
the minimal spanning tree of a weighted connected graph
G if its cost is minimum.