0% found this document useful (0 votes)
10 views15 pages

Unit 4

Uploaded by

asurideepika20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views15 pages

Unit 4

Uploaded by

asurideepika20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Introduction to Graphs

Graph is a non-linear data structure. It contains a set of points known as nodes (or vertices) and a set of links known as
edges (or Arcs). Here edges are used to connect the vertices. A graph is defined as follows...
Graph is a collection of vertices and arcs in which vertices are connected with arcs

Graph is a collection of nodes and edges in which nodes are connected with edges
Generally, a graph G is represented as G = ( V , E ), where V is set of vertices and E is set of edges.
Example
The following is a graph with 5 vertices and 6 edges.
This graph G can be defined as G = ( V , E )
Where V = {A,B,C,D,E} and E = {(A,B),(A,C)(A,D),(B,D),(C,D),(B,E),(E,D)}.

Graph Terminology
We use the following terms in graph data structure...

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.

Edge
An edge is a connecting link between two vertices. Edge is also known as Arc. An edge is represented as
(startingVertex, endingVertex). 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)).

Edges are three types.

1. Undirected Edge - An undirected egde is a bidirectional edge. If there is undirected edge between vertices A and
B then edge (A , B) is equal to edge (B , A).
2. Directed Edge - A directed egde is a unidirectional edge. If there is directed edge between vertices A and B then
edge (A , B) is not equal to edge (B , A).
3. Weighted Edge - A weighted egde is a edge with value (cost) on it.

Undirected Graph
A graph with only undirected edges is said to be undirected graph.

Directed Graph
A graph with only directed edges is said to be directed graph.

Mixed Graph
A graph with both undirected and directed edges is said to be mixed graph.

End vertices or Endpoints


The two vertices joined by edge are called end vertices (or endpoints) of that edge.

Origin
If a edge is directed, its first endpoint is said to be the origin of it.

Destination
If a edge is directed, its first endpoint is said to be the origin of it and the other endpoint is said to be the destination of
that edge.

Adjacent
If there is an edge between vertices A and B then both A and B are said to be adjacent. In other words, vertices A and B
are said to be adjacent if there is an edge between them.

Incident
Edge is said to be incident on a vertex if the vertex is one of the endpoints of that edge.

Outgoing Edge
A directed edge is said to be outgoing edge on its origin vertex.

Incoming Edge
A directed edge is said to be incoming edge on its destination vertex.

Degree
Total number of edges connected to a vertex is said to be degree of that vertex.

Indegree
Total number of incoming edges connected to a vertex is said to be indegree of that vertex.

Outdegree
Total number of outgoing edges connected to a vertex is said to be outdegree of that vertex.

Parallel edges or Multiple edges


If there are two undirected edges with same end vertices and two directed edges with same origin and destination,
such edges are called parallel edges or multiple edges.

Self-loop
Edge (undirected or directed) is a self-loop if its two endpoints coincide with each other.

Simple Graph
A graph is said to be simple if there are no parallel and self-loop edges.

Path
A path is a sequence of alternate vertices and edges that starts at a vertex and ends at other vertex such that each
edge is incident to its predecessor and successor vertex.

Graph Representations

Graph data structure is represented using following representations...

1. Adjacency Matrix
2. Incidence Matrix
3. Adjacency List

Adjacency Matrix
In this representation, the graph is represented using a matrix of size total number of vertices by a total number of
vertices. That means a graph with 4 vertices is represented using a matrix of size 4X4. In this matrix, both rows and
columns represent vertices. This matrix is filled with either 1 or 0. Here, 1 represents that there is an edge from row
vertex to column vertex and 0 represents that there is no edge from row vertex to column vertex.

For example, consider the following undirected graph representation...

Directed graph representation...


Graph Traversal - DFS
Graph traversal is a technique used for a searching vertex in a graph. The graph traversal is also used to decide the
order of vertices is visited in the search process. A graph traversal finds the edges to be used in the search process
without creating loops. That means using graph traversal we visit all the vertices of the graph without getting into
looping path.

There are two graph traversal techniques and they are as follows...

1. DFS (Depth First Search)


2. BFS (Breadth First Search)

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.

Example
Graph Traversal - BFS

Graph traversal is a technique used for searching a vertex in a graph. The graph traversal is also used to decide the
order of vertices is visited in the search process. A graph traversal finds the edges to be used in the search process
without creating loops. That means using graph traversal we visit all the vertices of the graph without getting into
looping path.

There are two graph traversal techniques and they are as follows...

1. DFS (Depth First Search)


2. BFS (Breadth First Search)

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

Example
Heap Sort Algorithm
here are two types of heap data structures and they are as follows...

1. Max Heap
2. Min Heap

Every heap data structure has the following properties...


Property #1 (Ordering): Nodes must be arranged in an order according to their values based on Max heap or Min
heap.

Max Heap
Max heap data structure is a specialized full binary tree data structure. In a max heap nodes are arranged based on
node value.

Max heap is defined as follows...


Max heap is a specialized full binary tree in which every parent node contains greater or equal value than
its child nodes.
Example
Above tree is satisfying both Ordering property and Structural property according to the Max Heap data structure.

Heap sort is one of the sorting algorithms used to arrange a list of elements in order. Heap sort algorithm uses one of
the tree concepts called Heap Tree. In this sorting algorithm, we use Max Heap to arrange list of elements in
Descending order and Min Heap to arrange list elements in Ascending order.

Step by Step Process

The Heap sort algorithm to arrange a list of elements in ascending order is performed using following steps...

 Step 1 - Construct a Binary Tree with given list of Elements.


 Step 2 - Transform the Binary Tree into Min Heap.
 Step 3 - Delete the root element from Min Heap using Heapify method.
 Step 4 - Put the deleted element into the Sorted list.
 Step 5 - Repeat the same until Min Heap becomes empty.
 Step 6 - Display the sorted list.

Example
Complexity of the Heap Sort Algorithm

To sort an unsorted list with 'n' number of elements, following are the complexities...

Worst Case : O(n log n)


Best Case : O(n log n)
Average Case : O(n log n)

Comparison of Sorting Methods

The comparison of sorting methods is performed based on the Time complexity and Space complexity of sorting
methods. The following table provides the time and space complexities of sorting methods. These Time and Space
complexities are defined for 'n' number of elements.

Sorting Time Complexity Worst Time Complexity Average Time Complexity Best Space
Method Case Case Case Complexity

Bubble Sort n(n-1)/2 = O(n2) n(n-1)/2 = O(n2) n(n-1)/2 = O(n2) Constant

Insertion Sort n(n-1)/2 = O(n2) n(n-1)/4 = O(n2) O(n) Constant

Selection Sort n(n-1)/2 = O(n2) n(n-1)/2 = O(n2) n(n-1)/2 = O(n2) Constant

Quick Sort n(n+3)/2 = O(n2) O(n log n) O(n log n) Constant

Heap Sort O(n log n) O(n log n) O(n log n) Constant

Merge Sort O(n log n) O(n log n) O(n log n) Depends

You might also like