0% found this document useful (0 votes)
2 views26 pages

DS 21 Graph Theory

The document provides an overview of graphs, including definitions, types (directed and undirected), and applications in various fields such as computer networks and maze representation. It discusses graph representations like adjacency matrices and lists, as well as traversal algorithms including Depth-First Search (DFS) and Breadth-First Search (BFS). The document also includes code examples for implementing DFS and BFS in C++.

Uploaded by

mamuneebg
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)
2 views26 pages

DS 21 Graph Theory

The document provides an overview of graphs, including definitions, types (directed and undirected), and applications in various fields such as computer networks and maze representation. It discusses graph representations like adjacency matrices and lists, as well as traversal algorithms including Depth-First Search (DFS) and Breadth-First Search (BFS). The document also includes code examples for implementing DFS and BFS in C++.

Uploaded by

mamuneebg
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/ 26

Data Structures

Lecture No. 21

Graph, DFS, BFS


Engr. Rashid Farid Chishti
http://youtube.com/rfchishti
http://sites.google.com/site/chis
hti International Islamic University H-10, Islamabad, Pakistan
Graphs
 Graphs are composed of Nodes (vertices) and Edges.

 A graph is a set of vertices (V) with links or edges (E) connecting them
 Linked lists, trees, and heaps are all special cases of graphs
 Notation: A graph G is a pair (V, E) i.e G = (V, E)
 Each edge is a pair (v1, v2), where v1, v2 are vertices in V
 In a directed graph (digraph), all edges have directions 2
 In an undirected graph, an edge does not have a direction
 For Example 1 3
Node
G = (V, E) or 6
where V={1, 2, 3, 4, 5, 6} Vertex

E={(1, 2), (1, 6), (2, 3), (3, 4), (4, 5), (4, 6), (5, 6)} 5 4
Edge
Directed vs Undirected Graphs
 If the order of edge pairs (v1, v2) matters then the graph is directed (also called
a digraph): (v1, v2) ≠ (v2, v1)
 If the order of edge pairs (v , v ) does not matter, the graph is called an
1 2
undirected graph: in this case, (v1 , v2) = (v2 , v1)
A Directed Graph v 1 v2 Undirected Graph v 1 v 2

 Vertex v1 is adjacent to vertex v2, if there is an edge (v1, v2)


 In a digraph, existence of edge (v1, v2) does not mean v1 is adjacent to v2
 In an undirected graph, existence of edge (v1, v152) means both v1 and v2 are
adjacent to each other. 1v 2 v
-4

Definitions
 Path: a sequence of vertices v1, v2 …, vn such that consecutive vertices vj , vj+1
have an edge between them.
Simple Path = v1, v2, v3 v4, v5
v1 v3 v4 Length = 5

v5

v2 v6

Loop (v6, v6)

 Length: length of a path is the number of edges in the path


 Loop: an edge from a vertex onto itself, denoted by (v, v).
Definitions
 Simple Path: a path in a graph is simple if all vertices are distinct
 v1 , v3 , v4 (a simple path)
 v1 , v3 , v4 , v6 , v3 (a path but not a simple path)
 Cycle: a cycle is a path of length at least 1 such that the first and the last
vertices are the same.
 v1 , v2 , v3 , v1 (A Simple Cycle)
 v1 , v2 , v3 , v1 , v1 , v3 (A Cycle but not Simple Cycle)
v1 v3 v4

v5

v2 v6
Definitions
 A undirected graph is connected if there is a path from every vertex to every
other vertex. A digraph with this property is strongly connected
 If a directed graph is not strongly connected, but underlying undirected graph
is connected then the directed graph is weakly connected

v1 v3 v1 v3 v1 v3

v2 v4 v2 v4 v2 v4

strongly connected weakly connected disconnected


Graph Applications

Course Prerequisites Representing a Maze


Nodes = courses Nodes = rooms
Directed edge = prerequisite Edge = door or passage
Computer Networks
Nodes = computers
Edges = transmission rates

Representing Electrical Circuits


Nodes = battery, switch, resistor, etc.
Edges = connections
Graph Applications
 Driving Map  Clash Free Datasheet
 Edge = Road  Edge = Paper
 Vertex = Intersection  Vertex = Date
 Edge weight = Time required to cover  Google Maps
the road  Edge = Roads
 Airline Traffic  Vertex = Location
 Vertex = Cities serviced by the airline  Dijkstra's Algorithm
 Edge = Flight exists between two cities  Prims's Algorithm
 Edge weight = Flight time or flight cost  Kruskal's Algorithm
 Computer networks
 Vertex = Switches, routers
 Edge = Communication links
 Edge weight = Delay, hop count, cost
Graph Implementation
 There are at least two ways of representing graphs:
A B
 The adjacency matrix representation
 The adjacency list representation

 An adjacency matrix for a graph with n vertices numbered


0,1,...,n-1 is an n by n array matrix such that matrix[i][j] is C D
1 (true) if there is an edge from vertex i to vertex j and 0 A Graph
(false) otherwise.
 When the graph is weighted, we can let matrix[i][j] be the A B C D
weight that labels the edge from vertex i to vertex j A 0 0 1 1
instead of simply 1, and let matrix[i][j] equal to ∞ instead B 0 0 0 0

of 0 when there is no edge from vertex i to vertex j C 1 0 0 1


D 1 0 1 0
 Adjacency matrix for an undirected graph is symmetrical.
 i.e. matrix[i][j] is equal to matrix[j][i]
Adjacency Matrix
Adjacency List
 An adjacency list for a graph with n vertices numbered 0,1,... ,n-
A B
1 consists of n linked lists.
 The ith linked list has a node for vertex j if and only if the graph 6
9
contains an edge from vertex i to vertex j.
 Adjacency list is a better solution if the graph is sparse. C
8
D
 Space requirement is O(|E| + |V|), which is linear in the size of
the graph. In an undirected graph each edge (v,w) appears in Undirected Weighted Graph
two lists. So space requirement is doubled.
A B C D
0 A C 6 D 9 NULL A ∞ ∞ 6 9
1 B B ∞ ∞ ∞ ∞
NULL
C 6 ∞ ∞ 8
2 C A 6 D 8 NULL D 9 ∞ 8 ∞
3 D
A 9 C 8 NULL
Its Adjacency Matrix
Adjacency List
Adjacency Matrix for a Digraph
A B

C D
A Diagraph

A B C D
0 A C D NULL
A 0 0 1 1
1 B NULL B 0 0 0 0
2 C C 0 0 0 1
D NULL
D 0 0 0 0
3 D NULL
Adjacency Matrix
Adjacency List
Adjacency Matrix vs Adjacency List
 Two common graph operations:
1. Determine whether there is an edge from vertex i to vertex j.
2. Find all vertices adjacent to a given vertex i.

 An adjacency matrix supports operation 1 more efficiently.


 An adjacency list supports operation 2 more efficiently.

 An adjacency list often requires less space than an adjacency matrix.


 Adjacency Matrix: Space requirement is O(|V|2)
 Adjacency List: Space requirement is O(|E| + |V|), which is linear in the size
of the graph.
 Adjacency matrix is better if the graph is dense (too many edges)
 Adjacency list is better if the graph is sparse (few edges)
Graph Traversals
 A graph-traversal algorithm starts from a vertex v, visits all of the vertices that
can be reachable from the vertex v.
 A graph-traversal algorithm visits all vertices if and only if the graph is
connected.
 A connected component is the subset of vertices visited during a traversal
algorithm that begins at a given vertex.
 A graph-traversal algorithm must mark each vertex during a visit and must
never visit a vertex more than once.
 Thus, if a graph contains a cycle, the graph-traversal algorithm can avoid

infinite loop.
 We look at two graph-traversal algorithms:
 Depth-First Search
 Breadth-First Search
Graph Construction

adjLists
1 3 4 5 6 9
2
2 3 7 8
3 4 9
8 3 10
4
10
5
11 2 4
6
12 1
7 7
8
9
9 6 5
10
10
11
11 12 11

12
8 Adjacency List Graph
Depth First Search

adjLists
9
1  2 3 4 5 6
2  3 7 8
3  4 9
8 3 10
4
 10
5
 11 2 4
6
 12 1
7
8
 7

9
 9
6 5
10
 10

11
 11

12  12 11

 8 Adjacency List Graph


Example 1: Depth First Search
#include <iostream>
#include <map>
#include <list>
using namespace std;
class Graph {
public:
map<int, bool> visited;
map<int, list<int> > adj;
void addEdge(int v, int w); // function to add an edge to graph
void DFS(int v); // DFS traversal of the vertices reachable from v
};

void Graph::addEdge(int v, int w){


adj[v].push_back(w); // Add w to v’s list.
}

void Graph::DFS(int v)
{
visited[v] = true; // Mark the current node as visited and
cout << v << " "; // print it
16 1
Example 1: Depth First Search
// Recur for all the vertices adjacent to this vertex
list<int>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i)
if (!visited[*i])
DFS(*i);
}

int main() {
Graph g; // Create a graph given in the above diagram
int Start = 1;
g.addEdge( 1, 2); g.addEdge(1, 3); g.addEdge(1, 4); g.addEdge(1, 5); g.addEdge(1,
6);
g.addEdge( 2, 3); g.addEdge(2, 7); g.addEdge(2, 8);
g.addEdge( 3, 4); g.addEdge(3, 9);
g.addEdge( 4,10); g.addEdge( 5,11);
g.addEdge( 6,12); g.addEdge( 8, 9);
g.addEdge( 9,10); g.addEdge(10,11); g.addEdge(12, 8);
cout << "Depth First Traversal (starting from vertex "<< Start << " ) \n";
g.DFS(Start);
system ("PAUSE"); return 0;
17 2
}
Breadth First Search
 A systematic search strategy Breath First Search
 Begin at a root node and inspect all the

neighboring nodes
 For each of those neighboring nodes in turn,

inspect their neighboring nodes which were


unvisited, and so on
Algorithm
 Start from node x
 Mark x as visited and Put it a in queue
 While queue is not empty do Wave Approach
 Get a node s from queue and show it
 Get all neighbor vertices of vertex s. Starting Point
 If a neighbor has not been visited, then
First Level
mark it visited and put it in a queue.
Second Level
Breadth First Search
Queue pop() push()
2
1
4
5
6
8
3
7
9
12
10
11 6
5
4
7
9
3
8
11
12
10 811
4
5
6
7910
12 9
5
6
7
8
12
11
10 6
7
8
9
10
11
12 8
9
12
11
10

adjLists
9
1  2 3 4 5 6
2  3 7 8
3  4 9
8 3 10
4
 10
5
 11 2 4
6
 12 1
7
8
 7

9
 9
6 5
10
 10

11
 11

12  12 11

 8 Adjacency List Graph


Example 2: Breadth First Search
#include <iostream>
#include <map>
#include <list>
using namespace std;
class Graph {
public:
map<int, bool> visited;
map<int, list<int> > adj;
list<int> queue;
void addEdge(int v, int w); // function to add an edge to graph
void BFS(int v); // BFS traversal of the vertices reachable from v
};

void Graph::addEdge(int v, int w){


adj[v].push_back(w); // Add w to v’s list.
}

20 1
Example 2: Breadth First Search
void Graph::BFS(int v)
{
visited[v] = true; // Mark the current node as visited and
queue.push_back(v);// and put it in a queue
list<int>::iterator i;// 'i' will be used to get all adjacent vertices of a vertex
while(!queue.empty()) {
v = queue.front();// Get a vertex from queue and print it
queue.pop_front();
cout << v << " ";
// Recur for all the vertices adjacent to this vertex
list<int>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i){
if (!visited[*i])
{
visited[*i] = true;
queue.push_back(*i);
}
}
}
}
21 2
Example 2: Breadth First Search
int main() {
Graph g; // Create a graph given in the above diagram
int Start = 1;
g.addEdge( 1, 2); g.addEdge(1, 3); g.addEdge(1, 4); g.addEdge(1, 5); g.addEdge(1,
6);
g.addEdge( 2, 3); g.addEdge(2, 7); g.addEdge(2, 8);
g.addEdge( 3, 4); g.addEdge(3, 9);
g.addEdge( 4,10); g.addEdge( 5,11);
g.addEdge( 6,12); g.addEdge( 8, 9);
g.addEdge( 9,10); g.addEdge(10,11);
g.addEdge(12, 8);
cout << "Breadth First Traversal (starting from vertex "<< Start << " ) \n";
g.BFS(Start);
system ("PAUSE");
return 0;
}

22 3
Breadth First Search (Applications)
 Finding the shortest path out of the maze (unweighted)
 Finding the minimum spanning tree (unweighted)
 Peer to peer (P2P) networks to locate the host
 Facebook to find friends within a given distance
 Web crawler for search engines

Depth First Search (Applications)


 Maze generator by using randomized DFS
 Solving maze puzzles with one solution
 Topological sorting
Maze Solver
 Finding the shortest path out of the maze (unweighted)
 Represent corners, junctions, dead ends by nodes
 Use BFS from a starting point until an exit point is reached
 Start at the root and explore as far as possible along each
branch before backtracking
ALGORITHM dfs(x)
visit(x)
FOR each neighbor y of x DO
IF y was not visited yet THEN
dfs(y) Edge = door or passage
END
END
END
Nodes = rooms
Maze Solver (Using BFS)
 Finding the shortest path out of
the maze (unweighted)
 Represent corners, junctions,
dead ends by nodes
 Use BFS from a starting point
until an exit point is reached
 Start at the root and explore as
far as possible along each
branch before backtracking
ALGORITHM dfs(x)
visit(x)
FOR each neighbor y of x DO
IF y was not visited yet THEN
dfs(y)
END
END
END
Maze Generator (Using Random DFS)
 Randomly select a starting cell N
 Randomly select an unvisited neighbor (randomly choose N, S, E, W) W E
 Break the wall
 Continue until all cells are visited S

Star S N N W S 1 2 3 4 5 6
t E E S W E
E W W S W 7 8 9 10 11 12

N S W S W
W W E E N 13 14 15 16 17 18

N S N S E
E E E W E 19 20 21 22 23 24

W W S W E
N E E W S 25 26 27 28 29 30

S N W N E
Stop

You might also like