Professional Documents
Culture Documents
P VASUKI
Definition
• Graph is a set of items connected by
edges. Each item is called a vertex or
node.
B
A
C
D E
Applications
• The Graph ADT is used to represent
the relationship
• Example represents connectivity –
relationship with neighbour.
– the network may be a communications
or transportation network (of pipelines,
roads, etc.)
Applications
• Typical graph-related problems are
to determine
– accessibility in a network
– whether removal of a node will
disconnect a network
– the cheapest path between nodes
– whether a task is an indirect
prerequisite of another
Specialization
• directed graph
• undirected graph
• acyclic graph
• directed acyclic graph
• planar graph
• connected graph
• biconnected graph
Specialization
• complete graph
• dense graph
• sparse graph
• labeled graph
• weighted graph
• tree.
Notations of a graph
• V: the set of vertices of a graph
• E: the set of edges of a graph
• To say G = (V,E) is to say that
– graph G has vertex set V and edge set E
• n: the number |V| of vertices of the graph
• e: the number |E| of edges of the graph
• Note that if there are no multiple edges, then
e is O(n2)
– that is, |E| is O(|V|2)
Representation of a graph
a. Logical Representation Y
X
Nod X Y z
c. Adjacency Matrix e
In weighted graph the weight x 0 1 1
represents cost. In matrix y 1 0 0
‘1’ is replaced by the cost
z 1 0 0
• Find out Time and Space complexity
in Adjacency List and Adjacency
Matrix Representation!!
Adj List and Adj Matrix
• An adjacency matrix requires Θ(n) space
– finding a single entry takes O(1) time;
processing all vertices adjacent to a vertex v
takes Θ(n) time
• The adjacency lists of a graph, taken together,
require Θ(n+e) space
– The first operation above might visit all nodes
adjacent to v; doing so might take Θ(n) time
• For a dense graph (where e is Θ(n2)), n+e is
Θ(n2) . If instead the graph is a sparse graph, an
adjacency list is more efficient.
Path
• Vertex w is adjacent to v in a graph iff (v,w) is in
the graph
– Note that this includes the case of an
undirected graph with an edge between v and
w
• A path in a graph is a sequence (vi) such that
vi+1 is adjacent to vi for every i
• A path of n vertices may also be considered as a
sequence of n-1 edges.
• The length of a path generally refers to the
number of edges in the path
Graph Traversal
B
A C
D E
• Visiting in children in the subsequent level, before
visiting sibling. Depth First Search (DFS) –Preorder
traversal – uses stack data structure
• A->B->C>D->E
Node Indegree
A 0
Node Indegree
B 1
A 0
C 1
B 2
D 0
C 1 E 0
D 0
E 0 Output List : D,A, E