0% found this document useful (0 votes)
17 views

Lecture 16

The document discusses different types of graphs and graph traversal algorithms. It defines key graph terminology and covers common graph types including directed/undirected graphs. Graph traversal algorithms like breadth-first search and depth-first search are explained with examples.

Uploaded by

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

Lecture 16

The document discusses different types of graphs and graph traversal algorithms. It defines key graph terminology and covers common graph types including directed/undirected graphs. Graph traversal algorithms like breadth-first search and depth-first search are explained with examples.

Uploaded by

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

Course: Data Structure & Algorithm

By: Dr. Muhammad Fayaz


Lecture # 16

Department of Computer Science, University of Central Asia

1
RECAP
Array Operations
 Operation on array
• Traversing (accessing each element at least once)
• Insertion
• Deletion
• Searching
• Sorting
 Bubble Sort
 Selection Sort
 Insertion Sort
 Radix Sort
 Counting Sort
• Stack
 Polish Notation
 Reversion Polish Notation
 Evaluation
• Queue
• Link List
• Hash Function
• Tree
Outlines
 Graph Definition
 Graph Terminologies
 Graph Types
 Adjacency Matrix Representation
 Edge List Representation
 Implementations
 Applications

3
Types of Graph -
 A graph is a collection of nodes (or vertices, singular is vertex) and
edges (or arcs)
G = (V, E)

 In this graph,
 V={A,B,C,D,E}
 E = { AB , AC , BD , CD , DE }
TERMENOLOGIES
Terminologies
 Node/Vertex: Each element of a graph is called node/vertex of a graph
 Edge/Arc: Line joining two nodes is called an edge
Terminologies
 Degree of a Node: The degree of a node is the number of edges it has.
 Parallel Edges: Two or more edges joining a pair of vertices
 Example:

A graph with vertices labelled by degree A and B nodes are joined by two parallel edges
Terminologies
 Graph Size: The size of a graph is the number of edges in it, it is denoted as
|E|
 Order of Graph: The order of the graph is the total number of nodes, it is
denoted as : |V|
 Neighbor Vertices / Adjacent Vertices: If two nodes are connected by an
edge, they are neighbors (and the nodes are adjacent to each other).
 Example:

A and B are adjacent nodes


A and D are adjacent nodes
D and C are adjacent nodes
a. |V| = 10 B and C are adjacent nodes
A and C are not adjacent
Terminologies
 Complement of Graph: The following example shows a graph G and
its complement graph G’-
Terminologies
 Path: A path is a list of edges such that each node (except the last) is the
predecessor of the next node in the list
 Cycle: A cycle is a path whose first and last nodes are same

6
Birmingham Rugby
0  Example: (London, Bristol,
150 Birmingham, London, Dover) is a
190 100
140 Cambridge path
London  Example: (London, Bristol,
190 120
Birmingham, London) is a cycle
Bristol 110 Dover

Southampton
TYPES OF GRAPH
Types of Graph
 There are many various types of graph are-
Types of Graph -
 Non-Directed Graph –
 A graph in which all the edges are undirected is called as a non-directed graph.
 In other words, edges of an undirected graph do not contain any direction.
Example-

Here,
 This graph consists of four vertices and four undirected edges.
 Since all the edges are undirected, therefore it is a non-directed graph.
Types of Graph -
 Directed Graph –
 A graph in which all the edges are directed is called as a directed graph.
 In other words, all the edges of a directed graph contain some direction.
 Directed graphs are also called as digraphs.

Example-

Here,
 This graph consists of four vertices and four directed edges.
 Since all the edges are directed, therefore it is a directed graph.
Types of Graph -
 Connected Graph
 A graph in which we can visit from any one vertex to any other vertex is called
as a connected graph.
 In connected graph, at least one path exists between every pair of vertices.
Example-

Here,
 In this graph, we can visit from any one vertex to any other vertex.
 There exists at least one path between every pair of vertices.
 Therefore, it is a connected graph.
Types of Graph -
 Disconnected Graph
 A graph in which we can’t visit from any one vertex to any other vertex is called
as a connected graph.
Example-

Here,
 This graph consists of two independent components which are disconnected.
 It is not possible to visit from the vertices of one component to the vertices of
other component.
 Therefore, it is a disconnected graph.
Types of Graph -
 Regular Graph-
 A graph in which degree of all the vertices is same is called as a regular graph.
 If all the vertices in a graph are of degree ‘k’, then it is called as a “k-regular
graph“.
Example-

Here,
 In these graphs,
 All the vertices have degree-2.
 Therefore, they are 2-Regular graphs.
Binary Search Tree -
 Complete Graph-
 A graph in which exactly one edge is present between every pair of vertices is
called as a complete graph.
 A complete graph of ‘n’ vertices contains exactly n C2 edges.
 A complete graph of ‘n’ vertices is represented as Kn.
Example-

In these graphs,
 Each vertex is connected with all the remaining vertices through exactly one
edge.
Types of Graph -
 Acyclic Graph-
 A graph not containing any cycle in it is called as an acyclic graph.
Example-
Types of Graph -
 Weighted Graph
 A graph where each edge is assigned a numerical label or “weight”.

Example-
Binary Search Tree -
 Graphs have many applications in various fields, including:
 Computer Science: Graphs are used to represent data structures like trees, networks,
and databases.
 Social Networks: Graphs are used to model social networks like Facebook, Twitter,
LinkedIn, and Instagram.
 Transportation: Graphs are used to model transportation networks like road networks,
air traffic control, and subway systems.
 Biology: Graphs are used to model protein interactions, gene networks, and metabolic
pathways.
 Business: Graphs are used to model customer relationships, supply chains, and
financial transactions.
 Mathematics: Graphs are used in mathematics to model discrete structures like groups,
rings, and fields.
 Chemistry: Graphs are used to model chemical compounds and reactions.
 Physics: Graphs are used to model physical phenomena like particle interactions and
energy levels.
 Geography: Graphs are used to model terrain, climate, and ocean currents.
 Overall, graphs are a powerful tool for modeling and analyzing complex systems in
many different fields.
Graph Traversing
Graph Traversal
 Graph traversal is a fundamental operation in graph theory and computer science,
essential for exploring or searching a graph data structure.
 There are several methods for traversing a graph, each suited to different scenarios
and requirements.
 There are main two types of graph traversal methods

Types

Breath First Depth First


Search (BFS) Search (DFS)
Graph Traversal – Breath-First Search (BFS)
 Breadth-First Search (BFS) is a graph traversal algorithm that visits all the nodes of a graph in
breadth-first order, i.e., it visits all the nodes at the same level before moving to the next level.
 To perform BFS, we need to maintain a queue of nodes to be visited.

BFS Algorithm
 A standard BFS implementation puts each vertex of the graph into one of two categories:
 Visited
 Not Visited
 The purpose of the algorithm is to mark each vertex as visited while avoiding cycles.
 The algorithm works as follows:

1. Start by putting any one of the graph's vertices at the back of a queue.
2. Take the front item of the queue and add it to the visited list.
3. Create a list of that vertex's adjacent nodes. Add the ones which aren't in the visited list to the back
of the queue.
4. Keep repeating steps 2 and 3 until the queue is empty.

 The graph might have two different disconnected parts so to make sure that we cover every
vertex, we can also run the BFS algorithm on every node
Graph Traversal – Breath-First Search (BFS)
 Example:
Graph Traversal – Breath-First Search (BFS)
 Example:
Graph Traversal – Breath-First Search (BFS)
 Example:
Graph Traversal – Breath-First Search (BFS)
 Example:
Graph Traversal – Breath-First Search (BFS)
 Example:
Graph Traversal – Depth First Search (DFS)
 Depth first Search or Depth first traversal is a recursive algorithm for searching all the
vertices of a graph or tree data structure. Traversal means visiting all the nodes of a
graph.

DFS Algorithm
 A standard DFS implementation puts each vertex of the graph into one of two
categories:
 Visited
 Not Visited
 The purpose of the algorithm is to mark each vertex as visited while avoiding cycles.
 The algorithm works as follows:

1. Start by putting any one of the graph's vertices on top of a stack.


2. Take the top item of the stack and add it to the visited list.
3. Create a list of that vertex's adjacent nodes. Add the ones which aren't in the visited list to
the top of the stack.
4. Keep repeating steps 2 and 3 until the stack is empty.
Graph Traversal – Depth First Search (DFS)
Example:
Graph Traversal – Depth First Search (DFS)
Example:
Graph Traversal – Depth First Search (DFS)
Example:
Graph Traversal – Depth First Search (DFS)
Example:
Representation of Graph in
Computer Memory
Adjacency-matrix representation
 Undirected
 Unweighted  One simple way of representing
A a graph is the adjacency matrix
B C  A 2-D array has a 1 at [i][j] if
there is an edge from node i to
D E
node j
F  The adjacency matrix is
G
symmetric about the main
AB CD EF G
diagonal
A • This representation is only
B suitable for small graphs!
C
D
E
F
G

36
Adjacency-matrix representation
Adjacency-matrix representation
Adjacency-matrix representation
Adjacency matrix representation- Directed Graph
Adjacency-matrix representation-Directed Graph
Adjacency matrix representation-Directed and
Weighted
Adjacency matrix representation-Directed and
Weighted
Adjacency matrix representation-Directed and
Weighted
Adjacency-matrix representation-Directed and Weighted

a b c d e
a 0 3 0 0 5
b 0 0 8 5 3
c 0 0 0 0 0
d 0 0 2 0 0
e 0 0 0 4 0
Edge List for Unweighted Undirected Graph
 An edge list is a data structure used to represent a graph as a list of
its edges.
 An (unweighted) edge is defined by its start and end vertex, so each edge
may be represented by two numbers.
 The entire edge list may be represented as a two-column matrix

S D
1 2
1 2 3
1 4
2 3
2 5
4 5 3 5
4 5
Edge List for Directed Graph
Edge List for Directed Graph
S D
1 2
1 3
1 4
2 4
2 5
3 6
4 3
4 6
4 7
5 4
5 7
7 6
Edge List for Undirected Weighted Graph

S D Weight
1 2 2
1 4 5
2 3 14
2 4 5
2 5 4
3 5 34
4 5 58
Edge List for Undirected Weighted Graph

S D Weight
A C 10
A D 60
B A 10
C B 20
C D 32
E A 7
Activity
 Find the shortest path from node 1 to node 17 using
 Adjacency matrix
 Edge List

Note: You can use any programing language for implementation


THANKS

You might also like