You are on page 1of 32

Data Structures - II

Dr. Nunna Satya Krishna

Department Of CSE
SRM University-AP

29 novembre 2023

Dr. N. S. Krishna DS-II 29 novembre 2023


UNIT-IV : GRAPHS

Dr. N. S. Krishna DS-II 29 novembre 2023


Topic : Graphs Introduction

Dr. N. S. Krishna DS-II 29 novembre 2023


Graph Introduction

» A graph data structure is a collection of nodes that have data and are
connected to other nodes.
» These nodes are called vertices, and line segments are called as arcs
or edges.
Definition
More precisely, a graph is a data structure (V, E) that consists of
» A collection of vertices V.
» A collection of edges E, represented as ordered pairs of vertices (u,v).
» Ex :
G = {V , E }, where V = {0, 1, 2, 3}, E = {( 0, 1), ( 0, 2), ( 0, 3), ( 1, 2)}

Dr. N. S. Krishna DS-II 29 novembre 2023


Graph Basic Terminology

Adjacency
A vertex is said to be adjacent to another vertex if there is an edge
connecting them. Vertices 2 and 3 are not adjacent because there is no
edge between them in the following example.

Path
A sequence of edges that allows you to go from vertex A to vertex B is
called a path.
Ex : 0-1, 1-2 and 0-2 are paths from vertex 0 to vertex 2 in the above
graph.
Dr. N. S. Krishna DS-II 29 novembre 2023
Graph Basic Terminology

Directed and Undirected Graphs


Graphs may be directed or undirected. In a directed graph, each line,
called an arc, has a direction indicating how it may be traversed. In an
undirected graph, the line is known as an edge, and it may be traversed in
either direction.

Dr. N. S. Krishna DS-II 29 novembre 2023


Graph Basic Terminology

» A path is a sequence of vertices in which each vertex is adjacent to


the next one. In above example, {A , B , C , E } is one path and
{A , B , E , F } is another path.
» Two vertices in a graph are said to be adjacent vertices (or
neighbors) if there is a path of length 1 connecting them. In above
example, B is adjacent to A, whereas E is not adjacent to D.
» A cycle is a path consisting of at least three vertices that starts and
ends with the same vertex. In above example(b), B, C, D, E, B is a
cycle.
» A loop is a special case of a cycle in which a single arc begins and
ends with the same vertex.

Dr. N. S. Krishna DS-II 29 novembre 2023


Graph Basic Terminology

» Two vertices are said to be connected if there is a path between


them.
» A graph is said to be connected graph if there is a path from any
vertex to any other vertex (ignoring direction).
» A directed graph is a strongly connected graph if there is a path
from each vertex to every other vertex in the digraph.
» A directed graph is weakly connected graph if at least two vertices
are not connected.
Note : A connected undirected graph would always be strongly
connected, so the concept is not normally used with undirected
graphs.
» A graph is a disjoint graph if it is not connected.

Dr. N. S. Krishna DS-II 29 novembre 2023


Primitive Graph Operations

1. Insert a vertex
2. Delete a vertex
3. Add an edge
4. Delete an edge
5. Find a vertex
6. Traverse a graph.

Dr. N. S. Krishna DS-II 29 novembre 2023


Graph Operations : Insert Vertex

» insertVertex() adds a new vertex to a graph.


» When a new vertex is added, it is disjoint ; that is, it is
not connected to any other vertices in the graph.

Dr. N. S. Krishna DS-II 29 novembre 2023


Graph Operations : Delete Vertex

» deleteVertex() removes a vertex from the graph.


» When a vertex is deleted, all connecting edges are
also removed.

Dr. N. S. Krishna DS-II 29 novembre 2023


Graph Operations : Add Edge

» addEdge() connects a vertex to a destination vertex


in the graph.
» If a vertex requires multiple edges, add an edge must
be called once for each adjacent vertex.
» To add an edge, two vertices must be specified.
» If the graph is a digraph, one of the vertices must be
specified as the source and one as the destination.

Dr. N. S. Krishna DS-II 29 novembre 2023


Graph Operations : Delete Edge

» deleteEdge() removes one edge from a graph.

Dr. N. S. Krishna DS-II 29 novembre 2023


Graph Operations : Find Vertex

» findVertex() traverses a graph, looking for a specified


vertex.
» If the vertex is found, its data are returned. If it is not
found, an error is indicated.

Dr. N. S. Krishna DS-II 29 novembre 2023


Graph Operations : Traversal

» Visiting all vertices ones in graph is nothing but


graph traversal.
» In most of the graph applications we need to visit the
graph vertices.
» Because a vertex in a graph can have multiple
parents, the traversal of a graph creates some
problems.
» There are two standard graph traversals :
1. Depth first traversal
2. Breadth first traversal
Dr. N. S. Krishna DS-II 29 novembre 2023
Graph Operations : Depth First Traversal

» In the depth first traversal, all of a node’s


descendents are processed before moving to an
adjacent node.

Dr. N. S. Krishna DS-II 29 novembre 2023


Graph Operations : Breadth First Traversal

» n the breadth-first traversal, all adjacent vertices are


processed before processing the descendents of a
vertex.

Dr. N. S. Krishna DS-II 29 novembre 2023


Topic : Graphs
Representations

Dr. N. S. Krishna DS-II 29 novembre 2023


Representation of Graphs

Graphs are commonly represented in two ways :


1. Adjacency Matrix
2. Adjacency List

Dr. N. S. Krishna DS-II 29 novembre 2023


Graph Representation : Adjacency Matrix

In the adjacency matrix representation, we use a vector to store the


vertices and a matrix to store the edges.

Dr. N. S. Krishna DS-II 29 novembre 2023


Graph Representation : Adjacency Matrix

Dr. N. S. Krishna DS-II 29 novembre 2023


Graph Representation : Adjacency List

Dr. N. S. Krishna DS-II 29 novembre 2023


Graph Structure

Dr. N. S. Krishna DS-II 29 novembre 2023


Depth First Search Traversal Algorithm

» Time Complexity : O ( V + E )

Dr. N. S. Krishna DS-II 29 novembre 2023


Depth First Search Traversal Algorithm

» Time Complexity : O ( V + E )

Dr. N. S. Krishna DS-II 29 novembre 2023


Breadth First Search Traversal Algorithm

» Time Complexity : O ( V + E )
Dr. N. S. Krishna DS-II 29 novembre 2023
Spanning Tree

Definition
A spanning tree is a sub-graph of an undirected connected graph, which
includes all the vertices of the graph with a minimum possible number of
edges. If a vertex is missed, then it is not a spanning tree.

1. The edges may or may not have weights assigned to them.


2. The total number of spanning trees with n vertices that can be
created from a complete graph is equal to n ( n − 2 ) .
3. If we have n = 4, the maximum number of possible spanning trees is
equal to 44 − 2 = 16. Thus, 16 spanning trees can be formed from a
complete graph with 4 vertices.

Dr. N. S. Krishna DS-II 29 novembre 2023


Example of a Spanning Tree

» To understand the spanning tree, let us consider the following


undirected graph G.

» Some of the possible spanning trees that can be created from the
above graph are :

Dr. N. S. Krishna DS-II 29 novembre 2023


Minimum Spanning Tree

» A minimum spanning tree is a spanning tree in which the sum of the


weight of the edges is as minimum as possible.
» Example of a Spanning Tree : Consider the following graph.

» The possible spanning trees from the above graph are :

» The minimum spanning tree from the above spanning trees is :

Dr. N. S. Krishna DS-II 29 novembre 2023


Spanning Tree Applications
» Computer Network Routing Protocol
» Cluster Analysis
» Civil Network Planning

Minimum Spanning tree Applications


» To find the minimum paths in a map
» To design networks like telecommunication networks, water supply
networks, and electrical grids. Planning

Finding Minimum Spanning Tree


1. Prim’s Algorithm
2. Kruskal’s Algorithm

Dr. N. S. Krishna DS-II 29 novembre 2023


Minimum Spanning Tree : Prim’s Algorithm

» Time Complexity : O ( E log( V ))


Dr. N. S. Krishna DS-II 29 novembre 2023
Minimum Spanning Tree : Kruskal’s Algorithm

» Time Complexity : O ( E log( E ))

Dr. N. S. Krishna DS-II 29 novembre 2023

You might also like