Beruflich Dokumente
Kultur Dokumente
1 2 1 2
3
4 3
Fig.1(a) Fig.1(b)
Graphs
V(G1)= {1,2,3}
E(G1)= {(1,2),(1,3),(2,1), (2,3),(3,1),(3,2)}
V(G2)={1,2,3,4}
E(G2)={(1,2),(2,2),(2,3),(3,4),(4,1),(4,2),(4,3)}
1 3
deg+(1) = 2 1 3
deg+(2) = 3
deg+(3) = 2
L23 7
In-degree and out-degree of graph
For directed graph the in-degree and out-
degree of a vertex are the number of incoming
and outgoing edges respectively.
Example: the following figure has vertex 2 has
in-degree 3, out-degree 2.
1 2
4 3
Graphs
Complete graph: A simple graph in which
there exists an edge between every pair of
vertices is called a complete graph. Complete
graph of four vertices is shown in the figure. A
complete graph is sometimes also referred to
as a universal graph.
Example
Graphs
Degree of a graph: The maximum degree of
any vertex in a graph is called degree of a
graph.
Example: the degree of graph is 2
1 2
3
Representations of Graph
1.1 Adjacency matrix:
Let G = (V,E) be a graph with
n= |V| m= |E| and V = {V1, V2,, Vn}
G can be represented by an n x n matrix.
A=(aij) called the adjacency matrix for G.
A is defined by
Representations of Graph
aij = 1 vi vj E
0, otherwise for 1i,j h
if G is a weighted graph
aij = W(vi vj ) if vi vj E
C otherwise
c is a constant
Representations of Graph
Adjacency list:
An alternative to the adjacency matrix
representation is an array indexed by vector
number containing linked list is called
adjacency lists.
For each vertex vi, the ith array contains a list
with information on all edges of G that leave vi
Representations of Graph
(a) An undirected graph
1 2
5 6
3 4
Representations of Graph
0 1 1 0 0 0
1 0 0 1 1 0
1 0 0 1 0 0
0 1 10 1 0
0 1 0 1 0 1
0 0 0 0 1 0
Representations of Graph
Adjacency list structure of (a)
1 2 3 nil
2 1 4 5 nil
3
1 4 nil
4
2 3 5 nil
5
2 4 6 nil
6
5 nil
Representations of Graph
(a) A Weighted graph
1
25 2 16
5
11 6
5 3
18 4
14
32
6
Fig(2) Two representations for a weighted
directed graph
Representations of Graph
2 4 14 5 16 Nil
3
1 5 4 6 Nil
4
3 18 Nil
5
4 32 6 11 Nil
6 Nil
Representations of Graph
Directed acyclic graphs: A directed acyclic
graph, or DAG is a directed graph with no
cycles. The following figure is dag
1 2
4 3
Topological Sort
Topological sort: It is a process of assigning a
linear ordering to the vertices of a DAG so that if
there is an edge from vertex i to vertex j, then i
appears before j in the linear ordering.
Algorithm start with the node N, with zero
indegree ie., without any predecessors.
The algorithm will repeat the following steps until
the graph G is empty.
1. Finding a node N with zero in degree
2. Deleting N and its edge from the graph G.
Topological Sort
Algorithm for topological sort:
1. Find the indegree of each node N of G
2. Put in a queue all the nodes with zero
indegree.
3. Remove the front node N of the queue.
4.Repeat the following for each w adjacent to N
a) Reduce the indegree of w
b) if indegree of w is zero then add w to the
rear of the queue.
5.goto step 4 if queue is not empty.
6. end.
Consider the directed acyclic graph G in Fig 3.7
A C
B D
A D B .
8.Remove the front element B from the QUEUE.
9.Decrease by1,the indegree of each adjacent to B
Now indegree of C is 0
10.Add the node C with zero indegree now
A D B C
Remove the front element C from the queue.
The node C has no adjacent node . The queue has no
front element so, we can stop the process. The
required topological sort is as follows ADBC.
DEPTH FIRST SEARCH
Algorithm DFS(v)
{
Visited(v)=true
For each vertex w adjacent to v do
If(visited(w)true)then // if unvisited node
DFS(w)
}
Depth first search
The steps followed by the algorithm are
1.begin by marking 1 visited and invoking DFS(1)
2.Both 2 and 3 are adjacent to1 , on a left to right ,so
goto 2. Invoke DFS(2) and mark2 as visited.
3.Both 4 and5 are adjacent to 2 ,4 is encountered
first on a left to right ,so goto 4. Invoke DFS (4) and
mark 4 as visited.
4.Both 2 and 8 are adjacent to 4 .But only 8 is
unvisited adjacent node. so goto 8. Invoke DFS(8) and
mark 8 as visited.
5.Here 5,6,7 are adjacent to 8 .Goto 5 invoke DFS(5)
and mark 5 as visited.
Stack Implementation in DFS
BREADTH
FIRST SEARCH
BREADTH FIRST SEARCH