Sunteți pe pagina 1din 51

Chapter 7- Graphs

Prof. Ashwini Rao


Asst. Prof. IT
Graphs - Introduction
Is a non linear data structure.
A graph G consists of:
A set V of elements called nodes or vertices.
A set E of edges such that each edge e in E is
identified by a unique pair [u,v] of nodes in V,
denoted by e=[u,v].
Graph G, denoted as G=(V,E)
Graph Theory - Terminology
End points and adjacent nodes : if e=[u,v] , nodes u and v are called
end points of e, and u,v are said to be neighbors or adjacent nodes.
Degree of a node ,deg(u): Is the number of edges containing u.
Isolated node: If deg(u)=0, i.e u does not belong to any edge.
Path P of length n from a node u to a node v , is the sequence of n+1
nodes.
i.e. , P=(v0,v1,v2vn)
Closed path: if in path P, v0 = vn
Simple path : if all nodes are distinct, with the exception that v0
may equal vn.
Cycle : Is a closed simple path with length 3 or more. Cycle of
length k is called k-cycle.
Graph Theory - Terminology

In G = ( V , E ) ,
V = {A,B,C,D,E,F} and
E = (A,B), (A,C), (A,D), (B,D), (C,D),
(B,E), (E,D)}

Degree of :
A is 3, Bis 3, C is 2, D is 4 ,E is 2 & F is 0
F
F Simple & Closed Path (Cycle) :
A->B>E->D->C->A

Closed Path :
B->E->D->A->C->D->B

Isolated Node: F
Types of Graph Directed
Also called as Digraph or graph.
Each edge e in G is assigned direction.
e is also called an arc.
Terms used:
e begins at u and ends at v.
u is the origin and v is the destination.
u is the predecessor and v is a successor or neighbour of u.
u is adjacent to v and v is adjacent to u.
Out degree of u is number of edges beginning at u.
In degree of u is number of edges ending at u.
u is called a source if it has +ve out degree but zero in degree.
u is called a sink if it has zero out degree but a +ve in degree
(Node C).
Types of Graph Directed
A directed graph G is connected, or strongly connected if for each
pair u,v of nodes in G, there is a path from u to v and there is also
a path from v to u (i.e where any pair of vertices in the graph is
connected by at least one path )
Example: Directed Graph

Directed graph, G with 4 nodes and 7 directed edges.


e2 and e3 are parallel edges(begins at B and ends at A).
e7 is a loop (begins and ends ta the same point)
G is not strongly connected as there is no path from C to any node.
No node in G is a source.
Other types of Graphs
Complete Graph
If every node u in G is adjacent to every other node v
in G.
Such a graph will have n(n-1) /2 edges.
(7*6)/2=21 edges
Other types of Graphs Tree
Graph
Connected graph T without any cycles.
That is an unique simple path P between
any nodes u and v in T.
Other types of Graphs
Labeled or Weighted Graph
If edges of a graph are assigned data.
That is if each edge e in G is assigned a non negative
numerical value w(e) called weight or length of e.
Other types of Graphs
Multi Graph
Multiple edges: distinct edges e and e` are called
multiple edges if they connect the same end points.
i.e. if e=[u,v] and e`=[u,v].
Loops: an edge e is called a loop if it has identical
endpoints. i.e. if e=[u,v]
Representation of Graphs in
Memory Sequential
By means of an Adjacency Matrix.
If G is a simple directed graph with m nodes and suppose
nodes of G have been ordered and are called v0,v1,v2vm, ,
then adjacency matrix A=aij of the graph G is the mm
matrix defined as follows:
aij = 1 if vi is adjacent to vj , i.e there is an edge
(vi,vj)
aij = 0 Otherwise
Such a matrix is called a Bit matrix or a Boolean Matrix.
Different ordering of nodes may result in different
adjacency matrix.
Representation of Graphs in
Memory Sequential
Example:

X Y Z W

Adjacency Matrix of the graph is : Y


Z

W
Representation of Graphs in
Memory Sequential
Disadvantages:
Difficult to insert and delete nodes in G, as the size of the
array may need to be changed and the nodes may need to be
reordered.
If the number of edges in the graph is less, a great deal of
space will be wasted i.e. the matrix will be Sparse.
Representation of Graphs in
Memory Linked
Also called as Adjacency structure.
Contains two lists or files:
A node list NODE
An edge list EDGE
NODE List: each element in the list NODE will correspond to a
node in G and will be a record in the following form.

NODE- name or key value of node


NEXT pointer to the next node in the list NODE
ADJ pointer to the first element in the adjacency list of the
node, maintained by list EDGE
Representation of Graphs in
Memory Linked
EDGE List: each element in the list EDGE will correspond to an
edge in G and will be a record in the following form.

DEST- point to the location in the list NODE of the


destination or terminal node of the edge.
LINK links together the edges with same initial node.
Representation of Graphs in
Memory Linked
Representation of Graphs in
Memory Linked
Example : Representation of
Graphs in Memory Linked
Traversing a Graph
Breadth First Search (BFS) uses a queue data structure.
Depth First Search (DFS) uses a stack data structure.
For both the algorithms, each node N of G will be in one of the
3 states called status of N.
Status=1: (Ready state) The initial state of the node N.
Status=2: (Waiting state) The node N is on the queue/stack
waiting to be processed.
Status=3: (Processed state) The node N has been
processed.
Breadth First Search (BFS)
Example - BFS
To find all nodes reachable from node A

A Adjacency Lists
A F,C,B
B G,C
F C B C F
D C
E D,C,J
D E G
F D
G C,E
J K J D
K E,G,J
BFS Start with Node A

C B A
F

E G Add A to the queue


D Front =1
Rear =1

J K
BFS Start with Node A

C B A,F,C,B
F

E G Remove A and process it


D Front =2
Rear =4

J K

A
BFS Start with Node A
A

C B A,F,C,B,D
F

E G Remove F and process it


D Front =3
Rear =5

J K

A,F
BFS Start with Node A

C B A,F,C,B,D
F

E G Remove C and process it


D Front =4
Rear =5

J K

A,F,C
BFS Start with Node A

C B A,F,C,B,D,G
F

E G Remove B and process it


D Front =5
Rear =6

J K

A,F,C,B
BFS Start with Node A

C B A,F,C,B,D,G
F

E G Remove D and process it


D Front =6
Rear =6

J K

A,F,C,B,D
BFS Start with Node A

C B A,F,C,B,D,G,E
F

E G Remove G and process it


D Front =7
Rear =7

J K

A,F,C,B,D,G
BFS Start with Node A

C B A,F,C,B,D,G,E,J
F

E G Remove E and process it


D Front =8
Rear =8

J K

A,F,C,B,D,G,E
BFS Start with Node A

C B A,F,C,B,D,G,E,J
F

E G Remove J and process it


D Front =9
Rear =8
No more unprocessed nodes
J K Process ends!!!!

A,F,C,B,D,G,E,J Are the nodes reachable from node A


Depth First Search (DFS)
DFS: Start with Node J
Adjacency Lists
A
A F,C,B

B G,C

C F
F C B
D C

E D,C,J

D E G F D

G C,E

J D,K
J K
K E,G
DFS: Start with Node J

F C B

D E G
J
J K
Push J
DFS: Start with Node J

F C B

D E G

J K
Pop/Visit/Mark J
J
DFS: Start with Node J

F C B

D E G K
D
J K
Push neighbors of J
D,K
J
DFS: Start with Node J

F C B

D E G
D
J K Pop/Visit/Mark K

J, K
DFS: Start with Node J

F C B
G
E G E
D
D
J K
Push neighbors of K -
E, G,
J,K
DFS: Start with Node J

F C B

E G E
D
D
J K Pop/Visit/Mark G

J,K,G
DFS: Start with Node J

F C B
C

D E G E
D
J K
Push neighbors of G - C
J,K,G
DFS: Start with Node J

F C B

D E G E
D
J K
Pop/Visit/Mark C

J,K,G,C
DFS: Start with Node J

F C B
F

D E G E
D
J K
Push neighbors of C -
F
J,K,G,C,
DFS: Start with Node J

F C B

D E G E
D
J K
Pop/Mark/Visit F

J,K,G,C, F
DFS: Start with Node J

F C B

D E G E
D
J K
Push neighbors of F -
None
J,K,G,C, F
DFS: Start with Node J

F C B

D E G
D
J K
Pop/Mark/Visit E
J,K,G,C, F,E
DFS: Start with Node J

F C B

D E G
D
J K
Push neighbors of E -
None
J,K,G,C, F,E
DFS: Start with Node J
A

F C B

D E G

J K

J,K,G,C,F,E,D Pop/Mark/Visit D

Process stops Stack Empty !!!!


Nodes reachable from J are : J,K,G,C, F,E,D
Application of Graphs
Many travel web sites use graphs to find the shortest path between places.
Social network graphs: Graphs that represent who knows whom, who
communicates with whom, who influences whom or other relationships in social
structures. An example is the twitter graph of who follows whom. These can be
used to determine how information flows, how topics become hot, how
communities develop, or even who might be a good match for who, or is that
whom.
Website analysis - The best known example is the link graph of the web, where
each web page is a vertex, and each hyperlink a directed edge. Link graphs are
used, for example, to analyse relevance of web pages, the best sources of
information, and good link sites.
Transportation networks. In road networks vertices are intersections and edges
are the road segments between them. Such networks are used by many map
programs such as Google maps, Bing maps to find the best routes between
locations. They are also used for studying traffic patterns, traffic light timings,
and many aspects of transportation.
Application: Shortest Path
Algorithm (Dijkstras )
It presents a method to find the shortest path from
one point in the graph to any other node in the graph.
Used to find the shortest path in weighted graphs.
It is a greedy algorithm, i.e it solves the problem in
stages by doing what appears to be the best thing at
each stage.
Application: Shortest Path
Algorithm (Dijkstras )

S-ar putea să vă placă și