0% found this document useful (0 votes)
12 views7 pages

Ds Mod 5 Notes Part 1

The document provides an overview of graph theory, defining key concepts such as vertices, edges, and various types of graphs including directed, undirected, cyclic, and acyclic graphs. It explains graph representation methods like adjacency matrices and adjacency lists, and details traversal techniques including Depth First Search (DFS) and Breadth First Search (BFS) with accompanying algorithms. Additionally, it covers specific graph properties such as complete graphs, multi graphs, and the degree of a node.

Uploaded by

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

Ds Mod 5 Notes Part 1

The document provides an overview of graph theory, defining key concepts such as vertices, edges, and various types of graphs including directed, undirected, cyclic, and acyclic graphs. It explains graph representation methods like adjacency matrices and adjacency lists, and details traversal techniques including Depth First Search (DFS) and Breadth First Search (BFS) with accompanying algorithms. Additionally, it covers specific graph properties such as complete graphs, multi graphs, and the degree of a node.

Uploaded by

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

Module 5

AVL Trees, RB Tree, Splay Tree, B-Tree,


Graphs
A graph, G, consists of two sets, V and E. V is a finite, non-empty set of vertices. E is a set of pairs
of vertices, these pairs are called edges.
V(G) and E(G) will represent the set of vertices and edges of graph G.
Edges:

Each edge is defined by a pair of vertices

An edge connects the vertices that define it

Vertices:

Vertices also called nodes

Denote vertices with labels

Following are the three graph samples

In an undirected graph the pair of vertices representing any edge is unordered. Pair (u, v) and (v, u)
represent the same edge.
In a directed graph each edge is represented by a directed pair <u, v>. u is the tail and v are the head
of the edge. Pair (u, v) and (v, u) represent two different edges.
In the above diagram G1 and G2 are undirected
G3 is a directed Graph
Self-loop:
An edge which starts and ends on the same vertex

Raksha Puthran, Assistant Professor, SDIT


Multi graph:
A graph with multiple occurrences of the same edge between any two vertices is called multi graph.

Complete Graph:
A graph, if there exists an edge between every pair of vertices such graphs are said to be ‘complete’
otherwise the graph is said to be ‘not complete’.

Cyclic Graph: Graph that contains cycle


Acyclic Graph: Graph that does not contain cycle

Raksha Puthran, Assistant Professor, SDIT


Degree of a node:
Number of edges incident on the node
In-degree: number of incoming edges
Out-degree: number of outgoing edges
Path: A sequence of vertices from vertex ‘u’ to vertex ‘v’ in a directed/ undirected graph.
Length of the path: number of edges in the path.

Representation of graph:
Graph can be represented in two different ways:
Adjacency matrix
Adjacency linked list
Adjacency matrix
A square matrix (0,1) used to represent a finite graph
Consider the following three graphs:

Raksha Puthran, Assistant Professor, SDIT


Following is the adjacency matrix of the graphs G1, G3, G4

Adjacency list Representation


Following are the adjacency list of the graphs G1, G3, G4

Raksha Puthran, Assistant Professor, SDIT


Raksha Puthran, Assistant Professor, SDIT
Graph Traversal
There are two graph traversal techniques:
DFS (Depth first Search)
BFS (Breadth First Search)
Algorithm for 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 adjacent vertex of the vertex which is at top of the stack which is not visited and
push it on to the stack.

Step 4: Repeat step 3 until there are no new vertex to be visit from the vertex on top of the stack.

Step 5: When there is no new vertex to be 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

Consider the following function for depth first search:


void dfs (int v)
{
nodePointer w;
visited[v] = TRUE;
printf(“%d”, v);
for (w= graph[v] ; w ; w = w→ link)
if(! visited[w→ vertex])
dfs(w→ vertex);
}
Algorithm for 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 adjacent vertices of the vertex which is at front of the Queue which is not visited and
insert them into the Queue.

Step 4: When there is no new vertex to be visit from the vertex at front of the Queue then delete that vertex
from the Queue.

Step 5: Repeat step 3 and 4 until queue becomes empty.

Step 6: When queue becomes Empty, then the enqueue or dequeue order gives the BFS traversal order

Consider the following function for Breadth first search:

Raksha Puthran, Assistant Professor, SDIT


void bfs(int v)
{
nodePointer w;
front=rear=NULL;
printf(“%d”, v);
visited[v] = TRUE;
addq(v);
while(front)
{
v= deleteq();
for(w= graph[v]; w ; w=w→link)
if(! visited[w→vertex])
{
Printf(“%d”, w→ vertex);
addq(w→vertex);
visited[w→vertex] = TRUE;
}
}
}

Raksha Puthran, Assistant Professor, SDIT

You might also like