Вы находитесь на странице: 1из 43

DATA STRUCTURE USING C+

+
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

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

Graphs as one of the most important non-

linear data structures


The representation that models various

kinds of graphs
Some useful graph algorithms

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

A graph G is a discrete structure consisting of nodes


(vertices) and the lines joining the nodes (edges)
For finite graphs, V and E are finite. We can write a
graph as G = (V, E)

Graph traversals
Visiting all the vertices and edges in a
systematic fashion is called as a graph traversal

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

Graphs are classified as directed and undirected


graphs

In an undirected graph, an edge is a set of two


vertices where order does not make any relevance,
whereas in a directed graph, an edge is an ordered
pair

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

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

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

Representation of Graphs
Adjacency matrix (sequential

representation)
Adjacency list (linked representation)
Adjacency Multilist
Inverse Adjacency List

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

A graph in which every edge is directed is called

as a directed graph or diagraph


A graph in which every edge is undirected is
called as an undirected graph
If some of edges are directed and some are undirected
in a graph, the graph is called as mixed graph

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

Figure 1: Simple
graph

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

Any graph which that contains parallel edges is called

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

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

10

A graph in which weights are assigned to every edge is


called as a weighted graph

Weights can also be assigned to vertices

A graph of area and streets of a city may be assigned


weights according to its traffic density

A graph of areas and roads connecting may be assigned


distance between cities to edges and area population to
vertices

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

11

In a graph, a node which that is not

adjacent to any other node is called an


isolated node
A graph containing only isolated
nodes is called a null graph
A graph in which weights are assigned to
every edge is called as a weighted graph

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

12

Figure 3: Null Graph

Tuesday, May 12, 2015

Figure 4: Graph With Isolated Verte

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

13

In a directed graph, for any node V the number of edges


which that have V as their initial node is called outdegree of
the node V
In other words, the number of edges incident from node is
its outdegree (outgoing degree)
and the number of edges incident to it is an indegree
(incoming degree)
The sum of indegree and out degree is the total degree of a
node (vertex)
In an undirected graph, the total degree or degree of a
node is the number of edges incident with the node.
The isolated vertex degree is zero

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

14

A path which originates and ends at the same

node is called a cycle (circuit)


A cycle is elementary if each node is traversed
once (except origin) and is simple if every edge of
cycle is traversed once

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

15

Connectivity :

A graph is said to be a connected one if and


only if there exists a path between every pair of
vertices

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

16

Figure 5 (a): Graph with two


connected components

Figure 5 (b): Graph with one


connected component

Figure 5: Graph with connected


component

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

17

A simple graph which that does not have any

cycles is called acyclic graph


Such graphs do not have any loops

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

18

Adjacency Matrix
The graphs represented using a sequential

representation using matrices is called an


adjacency matrix

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

19

Adjacency Matrix
representation

Matrix
Representation

Fig a Graph

Fig b Matrix representation


for graph shown in fig a

Figure 1: Adjacency Matrix representation


Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

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

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

21

Fig : Graph

Fig : Adjacency List representation


for graph shown in fig a

Figure 2: Adjacency List representation


Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

22

Multiclass are lists where nodes may be


shared among several other lists

The node structure of such a list can be


represented as follows :

Figure 3: Node Structure for multilist

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

23

Fig a : Graph

Fig b: Multilist representation


for graph shown in fig a

Figure 4: Representation for Adjacency


multilist for graph

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

24

Inverse adjacency lists is a set of lists that

contain one list for vertex


Each list contains a node per vertex

adjacent to the vertex it represents

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

25

Figure a: Graph

Figure b: inverse Adjacency list


Representation

Figure 5: Representation for Graph


and its inverse adjacency list

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

26

Graph Traversals
Depth-First Search
Breadth-First Search

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

27

DFS starts at the vertex v of G as a start vertex and


v is marked as visited
Then, each unvisited vertex adjacent to v is
searched using the DFS recursively
Once all the vertices that can be reached from v
have been visited, the search of v is complete
If some vertices remain unvisited, we select an
unvisited vertex as a new start vertex and then
repeat the process until all the vertices of G are
marked visited

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

28

Figure 6:(a) Graph and its (b)


adjacency list representation

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

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

Figure b : Depth-First Search Representati

Figure 7: Representation for Depth-First Search for


graph

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

30

In BFS, all the unvisited vertices adjacent to

i are visited after visiting the start vertex i


and marking it visited
Next, the unvisited vertices adjacent to

these vertices are visited and so on until


the entire graph has been traversed

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

31

Figure a : Sample
graph

Figure b : Breadth-First Search Represent

Figure 8: Breadth-first search sequence for


the graph
Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

32

A tree is a connected graph with no cycles

A spanning tree is a sub-graph of G that has all


vertices of G and is a tree

A minimum spanning tree of a weighted graph G is


the spanning tree of G whose edges sum to
minimum weight

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

33

A tree is a connected graph with no cycles

A spanning tree is a sub-graph of G that has all


vertices of G and is a tree

A minimum spanning tree of a weighted graph G is


the spanning tree of G whose edges sum to
minimum weight

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

34

Figure. 9 Graph and spanning trees (a) Graph (b) Spanning tree (c)
Minimum spanning tree
Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

35

Connected
Components

An undirected graph is connected if there is at least


one path between every pair of vertices in the graph

A connected component of a graph is a maximal


connected sub-graph, that is, every vertex in a
connected component is reachable from the vertices
in the component

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

36

Following fig shows the Sample graph with one


connected component

Figure a

Following fig shows the Graph with two connected


components

Figure b
Figure. 10: Graph representation with connected Components
Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

37

Prims Algorithm

Prims algorithm starts from one vertex and grows


the rest of the tree by adding one vertex at a time,
by adding the associated edges

This algorithm builds a tree by iteratively adding


edges until a minimal spanning tree is obtained, that
is, when all nodes are added

At each iteration, it adds to the current tree a vertex


though minimum weight edge that does not
complete a cycle

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

38

Minimum Spanning Tree

Minimum
Spanning Tree

Figure a: Sample Graph

Figure b : Minimum Spanning Tree

Figure 11: representation of Minimum Spanning tree for given Graph

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

39

Kruskals Algorithm

Another way to construct a minimum spanning tree


for a graph G is to start with a graph T = (V, E = )
consisting of the n vertices of G and having no edges

In Prims algorithm, we start with one connected


component, add a vertex to have one connected
component and no cycles, and end up with one
connected component

Here, we start with n connected components; at


each step, the number of connected components
would reduce by one and end up with one connected
component

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

40

Shortest Path Algorithm

The shortest path between two given vertices is the


path having minimum length

This problem can be solved by one of the greedy


algorithms, by Edger W. Dijkstra, often called as
Dijkstras algorithm

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

41

Shortest Path Algorithm

Shortest Path

Directed weighted graph

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

42

Thank You !!!!!

Tuesday, May 12, 2015

Sachin Kumar Saxena, IT Dept. COER,


Roorkee

43

Вам также может понравиться