0% found this document useful (0 votes)
37 views18 pages

Graph PDF

A graph is a set of vertices connected by edges. It can be represented using adjacency lists or matrices and used to model relationships. Graph algorithms include finding shortest paths, determining connectivity, and topological sorting of tasks based on prerequisites. Topological sorting works by repeatedly selecting unvisited nodes with no prerequisites and adding them to the sorted list until all nodes are visited.

Uploaded by

sudhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views18 pages

Graph PDF

A graph is a set of vertices connected by edges. It can be represented using adjacency lists or matrices and used to model relationships. Graph algorithms include finding shortest paths, determining connectivity, and topological sorting of tasks based on prerequisites. Topological sorting works by repeatedly selecting unvisited nodes with no prerequisites and adding them to the sorted list until all nodes are visited.

Uploaded by

sudhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Graph

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

b. Adjacency list Node Adj List


X Y,Z
Y X
Z 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

• Visiting all siblings and then visit children in subsequent


level is Breadth First search BFS – Uses queue data
structure
• A->B->D-> C->E
Topological Sort
• Ordering the vertices according to the
topological order.
• An instance of the topological sort problem is a
set of tasks, each with a set of prerequisite
tasks
• The goal is to find a list of the tasks in which
each task follows all of its prerequisites
– this list is said to be sorted in topological
order
• We'll assume that the prerequisite information
is represented as a directed graph.
Algorithm- Topological Sort
• List all nodes with in degree (the number of nodes
pointing to it)
• Initialize the data structure to contain all nodes
from the graph that have no incoming edges.(
Which has no prerequisite else all pre
• Repeatedly
– delete a node from the structure for
scheduling, and add it to the end of the output
list
– decrement the number of unmet prerequisites
for all immediate successors of the node, and
– add to the structure any node which now has
no unmet prerequisites.
• Until the structure is empty
Topological Sort - Output
• At the end if all nodes are not
displayed in the output list, there
might be some cycle in the graph.

• Else the algorithm terminates


successfully.
Example
Node Indegree
B
A 1 A C
B 3
C 1
D E
D 0
E 1
Output List : D – Choose a node Node Indegree
with in degree zero. A 0
B 2
C 1
Reduce the in degree of adjacent D 0
nodes of the chosen node.
E 0
Example
Proceeding in the B
same way as A C
shown in previous
page
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

Output List : D,A


Example
Node Indegree
A 0 B
A C
B 0
C 1
D 0 D E
E 0
Output List : D, A, E, B
Thus D A E B C is the
Node Indegree topological order of
A 0 the given graph.
B 0
C 0
D 0
E 0

Output List : D,A, E, B, C

You might also like