Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
+
Algorithm and Design
UNIT 5
SACHIN KUMAR SAXENA
ASST. PROF.
DEPARTMENT OF INFORMATION TECHNOLOGY
COLLEGE OF ENGINEERING ROORKEE, ROORKEE
sachinsax@gmail.com
www.sachinplacement.blogspot.com
Tuesday, May 12, 2015
kinds of graphs
Some useful graph algorithms
Graph traversals
Visiting all the vertices and edges in a
systematic fashion is called as a graph traversal
Graph
create() :Graph
insert vertex(Graph, v)
:Graph
delete vertex(Graph, v)
:Graph
insert edge(Graph, u, v)
:Graph
delete edge(Graph, u, v)
:Graph
is empty(Graph) :Boolean;
end graph
Representation of Graphs
Adjacency matrix (sequential
representation)
Adjacency list (linked representation)
Adjacency Multilist
Inverse Adjacency List
Figure 1: Simple
graph
a Multigraph
On the other hand, a graph which has no
parallel edges is called as a simple graph
Figure 2: Multigraph
Tuesday, May 12, 2015
10
11
12
13
14
15
Connectivity :
16
17
18
Adjacency Matrix
The graphs represented using a sequential
19
Adjacency Matrix
representation
Matrix
Representation
Fig a Graph
20
Adjacency Lists
In an adjacency list, the n rows of the adjacency list
are represented as n-linked lists, one list per vertex
of the graph
We can represent G by an array Head, where Head[i]
is a pointer to the adjacency list of vertex i
Each node of the list has at least two fields: vertex
and link
The vertex field contains the vertexid, and link field
stores the pointer to the next node storing another
vertex adjacent to i
21
Fig : Graph
22
23
Fig a : Graph
24
25
Figure a: Graph
26
Graph Traversals
Depth-First Search
Breadth-First Search
27
28
29
Depth-First Search
Representation
The set V = {1, 2, 3, 6, 5, 7, 8, 9, 4} represents the order in which
they are visited. Hence, the DFS of the graph (Fig. a) gives the
sequence as 1, 2, 3, 6, 5, 7, 8, 9, and 4. This is shown in Fig. b
Figure a : Sample
graph
30
31
Figure a : Sample
graph
32
33
34
Figure. 9 Graph and spanning trees (a) Graph (b) Spanning tree (c)
Minimum spanning tree
Tuesday, May 12, 2015
35
Connected
Components
36
Figure a
Figure b
Figure. 10: Graph representation with connected Components
Tuesday, May 12, 2015
37
Prims Algorithm
38
Minimum
Spanning Tree
39
Kruskals Algorithm
40
41
Shortest Path
42
43