GRAPHS IN DATA
STRUCTURE
1
Definitio
ns
2
• Graph: A data structure that consists of a set of models and a
set of edges that relate the nodes to each other
• Vertex: A node in a graph
• Edge (arc): A pair of vertices representing a connection between
two nodes in a graph
• Undirected graph: A graph in which the edges have no direction
• Directed graph (digraph): A graph in which each edge is
directed from one vertex to another (or the same) vertex
Formall
y
3
A graph G is defined as follows:
G = (V,E)
where
V(G) is a finite, nonempty set of vertices
E(G) is a set of edges (written as pairs of
vertices)
4
Graph
terminology
• The size of a graph is the number of nodes in it
• The empty graph has size zero (no nodes)
• The degree of a node is the number of edges it has
• The in-degree of a node is the number of in-edges it has
• The out-degree of a node is the number of out-edges it has
• For directed graphs
• If a directed edge goes from node S to node D, we call S the source and D the
destination of the edge
• The edge is an out-edge of S and an in-edge of D
• S is a predecessor of D, and D is a successor of S
Graph
Terminology
What are the in-degrees and out-degrees of the vertices a, b, c, d in
this graph
a
b
c
d
deg-(a) = 1
deg+(a) = 2
deg-(b) = 4
deg+(b) = 2
deg-(d) = 2
deg+(d) = 1
deg-(c) = 0
deg+(c) = 2
5
• Example: What are the degrees of the vertices in the
graphs G and H?
Solutio
n:


deg(d)
 5
deg(a)  4
deg(b)  deg(e) 
6
In H 
deg(c) 
1
deg(g)
 0
deg(e) 
3
deg(b)  deg(c)  deg(f) 
4
deg(a) 
2

In G deg(d) 
1
a f e g
d
G H
6
e d
b c a b c
Graph
Terminology
Find the in-degree and the out-
degree of each vertex in the
graph G
Solution: The degree of G are:
deg-(a) =
2
deg-(b) =
2
deg-(c) =
3
deg-(d) =
2
deg-(e) =
deg+(a) =
4
deg+(b) =
1
deg+(c) =
2
deg+(d) =
2
deg+(e) =
a
7
e f
c
b
1. A vertex of degree 0 is called isolated, since
it is not adjacent to any vertex.
2. A vertex with a loop at it has at least degree
2 and, by definition, is not isolated, even if
it is not adjacent to any other vertex.
3. A vertex of degree 1 is
called pendant. It is
adjacent to exactly one other vertex.
8
Graph
terminology
• A cycle is a path whose first and last nodes are
the same
• Example: (London, Bristol, Birmingham, London, Dover) is a
path
• Example: (London, Bristol, Birmingham, London) is a cycle
• A cyclic graph contains at least one cycle
• An acyclic graph does not contain any
cycles
Birmingham Rugby
London
Cambridge
Bristol
Southhampton
Dover
60
140
190
150
190 100
120
110
An undirected
graph
9
A directed
graph
10
A directed
graph
11
More definitions :
Path
A list of vertices in which successive vertices are
connected by edges
A B C
B A C D
A B C A B C A B C D
B A B A C
A B
C
D
12
More definitions :Simple
Path
No vertex is
repeated.
A B C D
D C A
D C B
A B
A B C
A B
C
D
13
More definitions :
Cycle
Simple path with distinct edges, except that the first
vertex is equal to the last
A B C A
B A C
B C B
A C
A B
C
D
A graph without cycles is called acyclic
graph.
14
More definitions :
Loop
An edge that connects the vertex with
itself
A B
C
D
15
Graphs and
Trees
Tree: an undirected graph with no cycles, and a node
chosen to be the root
A B
C
E
D
Source
graph:
16
Graphs and
Trees
A
C
E B
C
A
E
B
D
17
D
Tree1: root A
Tree2: root C
A spanning tree of an undirected
graph
A sub-graph that contains all the vertices, and no cycles. If we add any
edge to the spanning tree, it forms a cycle, and the tree becomes a graph
A B
C
D
graph
A B
C
D
spanning tree
18
Example
s
A B
C
D
A B
C
D
A B
C
D
All spanning trees of the graph
on the previous slide
19
More
Definitions
20
• Adjacent vertices: Two vertices in a graph that are connected
by an edge
• Path: A sequence of vertices that connects two nodes in a
graph
• Complete graph: A graph in which every vertex is directly
connected to every other vertex
• Weighted graph: A graph in which each edge carries a value
Test Your
Knowledge
Cyclic or
Acyclic?
21
Two complete
graphs
22
Terminolo
gy
• A directed graph that has no cyclic paths is called a DAG (a Directed
Acyclic Graph).
23
Terminolo
gy
24
• An undirected graph is connected if a path exists from every
vertex to every other vertex
• A directed graph is strongly connected if a path exists from
every vertex to every other vertex
• A directed graph is weakly connected if a path exists from
every vertex to every other vertex, disregarding the direction
of the edge
Test Your
Knowledge
Connected, Strongly connected, or Weakly
connected
25
Connectivity: Are the following graphs
connected?
a
b
c
e
d
a
b
c
e
No.
d
Yes
.
a
b
c
e
d
Yes
.
d
a
b
c
e
f
No.
26
Terminolo
gy
• A graph is known as a weighted graph if a weight or metric is associated
with each edge.
27
A weighted
graph
28
Adjacency matrix – undirected
graphs
A B C D
A 0 1 1 1
B 1 0 0 1
C 1 0 0 1
D 1 1 0 0
Vertices:A,B,C,D
Edges: AC, AB, AD, BD
The matrix is symmetrical
A B
C
D
29
Adjacency matrix – directed
graphs
Vertices:
Edges:
A,B,C,D
AC, AB, BD, DA
A B C D
A 0 1 1 0
B 0 0 0 1
C 0 0 0 0
D 1 0 0 0
A B
C
D
30
Adjacencylists – undirected
graphs
Vertices:
Edges:
A,B,C,D
AC, AB, AD, BD
Heads lists
A. B C D
B. A D
C. A
D. A B
A B
D
C
31
Adjacency lists – directed
graphs
Vertices:
Edges:
A,B,C,D
AC, AB, BD, DA
-
Heads lists
A. B C
B. D
C
D
A
A B
C
D
32
Spanning
Tree
A spanning tree is a subset of
Graph G, which has all the
vertices covered with minimum
possible number of edges.
Hence, a spanning tree does not
have cycles and it cannot be
disconnected.
33
Minimum Spanning
Tree
A minimum spanning tree
(MST) of an edge-weighted
graph is a spanning tree
whose weight (the sum of the
weights of its edges) is no
larger than the weight of any
other spanning tree.
34
Minimum Spanning
Tree
MST is a tree because it is acyclic.
MST is spanning
because it covers
every edge.
MST is minimumfor the
obvious reason.
35
Minimum Spanning
Tree
36
• There are two basic algorithm to solve this
problem
1. Kruskal algorithm
2. Prim’s Algorithm
GRAPH TRAVERSAL
BFS (Breadth First Search) It is a tree traversal algorithm that
−
is also known as Level Order Tree Traversal. In this traversal we will
traverse the tree row by row i.e. 1st row, then 2nd row, and so
on.
DFS (Depth First Search ) It is a tree traversal algorithm that
−
traverses the structure to its deepest node. There are three most
used methods that are used to traverse the tree using DFS. it
goes into depth of each node as the root node and then goes to
the next one.

DSA THE LECTURE NUMMBER 213 OF AJKAKWWJWJNWJJWJWJWH .ppt

  • 1.
  • 2.
    Definitio ns 2 • Graph: Adata structure that consists of a set of models and a set of edges that relate the nodes to each other • Vertex: A node in a graph • Edge (arc): A pair of vertices representing a connection between two nodes in a graph • Undirected graph: A graph in which the edges have no direction • Directed graph (digraph): A graph in which each edge is directed from one vertex to another (or the same) vertex
  • 3.
    Formall y 3 A graph Gis defined as follows: G = (V,E) where V(G) is a finite, nonempty set of vertices E(G) is a set of edges (written as pairs of vertices)
  • 4.
    4 Graph terminology • The sizeof a graph is the number of nodes in it • The empty graph has size zero (no nodes) • The degree of a node is the number of edges it has • The in-degree of a node is the number of in-edges it has • The out-degree of a node is the number of out-edges it has • For directed graphs • If a directed edge goes from node S to node D, we call S the source and D the destination of the edge • The edge is an out-edge of S and an in-edge of D • S is a predecessor of D, and D is a successor of S
  • 5.
    Graph Terminology What are thein-degrees and out-degrees of the vertices a, b, c, d in this graph a b c d deg-(a) = 1 deg+(a) = 2 deg-(b) = 4 deg+(b) = 2 deg-(d) = 2 deg+(d) = 1 deg-(c) = 0 deg+(c) = 2 5
  • 6.
    • Example: Whatare the degrees of the vertices in the graphs G and H? Solutio n:   deg(d)  5 deg(a)  4 deg(b)  deg(e)  6 In H  deg(c)  1 deg(g)  0 deg(e)  3 deg(b)  deg(c)  deg(f)  4 deg(a)  2  In G deg(d)  1 a f e g d G H 6 e d b c a b c
  • 7.
    Graph Terminology Find the in-degreeand the out- degree of each vertex in the graph G Solution: The degree of G are: deg-(a) = 2 deg-(b) = 2 deg-(c) = 3 deg-(d) = 2 deg-(e) = deg+(a) = 4 deg+(b) = 1 deg+(c) = 2 deg+(d) = 2 deg+(e) = a 7 e f c b 1. A vertex of degree 0 is called isolated, since it is not adjacent to any vertex. 2. A vertex with a loop at it has at least degree 2 and, by definition, is not isolated, even if it is not adjacent to any other vertex. 3. A vertex of degree 1 is called pendant. It is adjacent to exactly one other vertex.
  • 8.
    8 Graph terminology • A cycleis a path whose first and last nodes are the same • Example: (London, Bristol, Birmingham, London, Dover) is a path • Example: (London, Bristol, Birmingham, London) is a cycle • A cyclic graph contains at least one cycle • An acyclic graph does not contain any cycles Birmingham Rugby London Cambridge Bristol Southhampton Dover 60 140 190 150 190 100 120 110
  • 9.
  • 10.
  • 11.
  • 12.
    More definitions : Path Alist of vertices in which successive vertices are connected by edges A B C B A C D A B C A B C A B C D B A B A C A B C D 12
  • 13.
    More definitions :Simple Path Novertex is repeated. A B C D D C A D C B A B A B C A B C D 13
  • 14.
    More definitions : Cycle Simplepath with distinct edges, except that the first vertex is equal to the last A B C A B A C B C B A C A B C D A graph without cycles is called acyclic graph. 14
  • 15.
    More definitions : Loop Anedge that connects the vertex with itself A B C D 15
  • 16.
    Graphs and Trees Tree: anundirected graph with no cycles, and a node chosen to be the root A B C E D Source graph: 16
  • 17.
  • 18.
    A spanning treeof an undirected graph A sub-graph that contains all the vertices, and no cycles. If we add any edge to the spanning tree, it forms a cycle, and the tree becomes a graph A B C D graph A B C D spanning tree 18
  • 19.
    Example s A B C D A B C D AB C D All spanning trees of the graph on the previous slide 19
  • 20.
    More Definitions 20 • Adjacent vertices:Two vertices in a graph that are connected by an edge • Path: A sequence of vertices that connects two nodes in a graph • Complete graph: A graph in which every vertex is directly connected to every other vertex • Weighted graph: A graph in which each edge carries a value
  • 21.
  • 22.
  • 23.
    Terminolo gy • A directedgraph that has no cyclic paths is called a DAG (a Directed Acyclic Graph). 23
  • 24.
    Terminolo gy 24 • An undirectedgraph is connected if a path exists from every vertex to every other vertex • A directed graph is strongly connected if a path exists from every vertex to every other vertex • A directed graph is weakly connected if a path exists from every vertex to every other vertex, disregarding the direction of the edge
  • 25.
    Test Your Knowledge Connected, Stronglyconnected, or Weakly connected 25
  • 26.
    Connectivity: Are thefollowing graphs connected? a b c e d a b c e No. d Yes . a b c e d Yes . d a b c e f No. 26
  • 27.
    Terminolo gy • A graphis known as a weighted graph if a weight or metric is associated with each edge. 27
  • 28.
  • 29.
    Adjacency matrix –undirected graphs A B C D A 0 1 1 1 B 1 0 0 1 C 1 0 0 1 D 1 1 0 0 Vertices:A,B,C,D Edges: AC, AB, AD, BD The matrix is symmetrical A B C D 29
  • 30.
    Adjacency matrix –directed graphs Vertices: Edges: A,B,C,D AC, AB, BD, DA A B C D A 0 1 1 0 B 0 0 0 1 C 0 0 0 0 D 1 0 0 0 A B C D 30
  • 31.
    Adjacencylists – undirected graphs Vertices: Edges: A,B,C,D AC,AB, AD, BD Heads lists A. B C D B. A D C. A D. A B A B D C 31
  • 32.
    Adjacency lists –directed graphs Vertices: Edges: A,B,C,D AC, AB, BD, DA - Heads lists A. B C B. D C D A A B C D 32
  • 33.
    Spanning Tree A spanning treeis a subset of Graph G, which has all the vertices covered with minimum possible number of edges. Hence, a spanning tree does not have cycles and it cannot be disconnected. 33
  • 34.
    Minimum Spanning Tree A minimumspanning tree (MST) of an edge-weighted graph is a spanning tree whose weight (the sum of the weights of its edges) is no larger than the weight of any other spanning tree. 34
  • 35.
    Minimum Spanning Tree MST isa tree because it is acyclic. MST is spanning because it covers every edge. MST is minimumfor the obvious reason. 35
  • 36.
    Minimum Spanning Tree 36 • Thereare two basic algorithm to solve this problem 1. Kruskal algorithm 2. Prim’s Algorithm
  • 37.
    GRAPH TRAVERSAL BFS (BreadthFirst Search) It is a tree traversal algorithm that − is also known as Level Order Tree Traversal. In this traversal we will traverse the tree row by row i.e. 1st row, then 2nd row, and so on. DFS (Depth First Search ) It is a tree traversal algorithm that − traverses the structure to its deepest node. There are three most used methods that are used to traverse the tree using DFS. it goes into depth of each node as the root node and then goes to the next one.