DS 21 Graph Theory
DS 21 Graph Theory
Lecture No. 21
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
v5
v2 v6
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
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.
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
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,
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
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
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