KEMBAR78
Graphs in data structure | PPTX
Data Structure
Graphs
Preston University Kohat
Hamza Javed
BCS 4th
2121-115001
Graphs
Introduction
• Graphs are a generalization of trees
• Nodes or vertices
• Edges or arcs
• Two kinds of graphs
• Directed
• Undirected
3
Introduction: Formal Definition
• A graph G = (V,E) consists of a finite set of vertices, V, and a finite set
of edges E.
• Each edge is a pair (v,w) where v, w  V
4
Introduction: Formal Definition
• A directed graph, or digraph, is a graph in which the edges are
ordered pairs
• (v, w) ≠ (w, v)
• An undirected graph is a graph in which the edges are unordered
pairs
• (v, w) == (w, v)
5
Introduction: Directed Graphs
• In a directed graph, the edges are arrows.
• Directed graphs show the flow from one node to another and not vise
versa.
6
Introduction: Undirected Graphs
• In a Undirected graph, the edges are lines.
• UnDirected graphs show a relationship between two nodes.
7
Terminology
• In the directed graph above, b is adjacent to a
because (a, b)  E. Note that a is not adjacent to b.
• A is a predecessor of node B
• B is a successor of node A
• The source of the edge is node A, the target is node B
8
Terminology
• In the undirected graph above, a and b are adjacent
because (a,b)  E. a and b are called neighbors.
9
Terminology
• A path is a sequence of vertices w1, w2,…wn such that
(wi, wi+1)  E, 1 <= i < n, and each vertex is unique
except that the path may start and end on the same
vertex
• The length of the path is the number of edges along
the path
10
Terminology
• An acyclic path is a path which does not follow a
sequence.
• A cyclic path is a path such that
• There are at least two vertices on the path
• w1 = wn (path starts and ends at same vertex)
• And also maintains the sequence
11
Test Your Knowledge
Cyclic or Acyclic?
12
Terminology
• A directed graph that has no cyclic paths is called a
DAG (a Directed Acyclic Graph).
• An undirected graph that has an edge between every
pair of vertices is called a complete graph.
Note: A directed graph can also be
a complete graph; in that case, there
must be an edge from every vertex
to every other vertex.
13
Test Your Knowledge
Complete, or “Acomplete” (Not Complete)
14
Test Your Knowledge
Complete, or “Acomplete” (Not Complete)
15
Terminology
• 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
16
Terminology
• A graph is known as a weighted graph if a weight or
metric is associated with each edge.
17
Representation of Graph
18
Following are the representation of a graph
• Adjacency Matrix
• Adjacency List
There are other representations also like, Incidence Matrix and
Incidence List. The choice of the graph representation is situation
specific. It totally depends on the type of operations to be performed
and ease of use.
19
Adjacency Matrix
• Adjacency Matrix is a 2D array of size V x V where V is the number of
vertices in a graph. Let the 2D array be adj[][], a slot adj[i][j] = 1
indicates that there is an edge from vertex i to vertex j. Adjacency
matrix for undirected graph is always symmetric. Adjacency Matrix is
also used to represent weighted graphs. If adj[i][j] = w, then there is
an edge from vertex i to vertex j with weight w.
20
Adjacency Matrix
21
Adjacency List
• An array of linked lists is used. Size of the array is equal to number of
vertices. Let the array be array[]. An entry array[i] represents the
linked list of vertices adjacent to the ith vertex. This representation
can also be used to represent a weighted graph. The weights of edges
can be stored in nodes of linked lists. Following is adjacency list
representation of the above graph.
22
Adjacency List
23
No
de
Adjacency List
A
B
C
D
E
B,C,D
C
C,E
C
a) Graphs G
b) Adjacency lists of G
24
25
Uses For Graphs
26
Uses for Graphs
• Computer network: The set of vertices V represents the set of
computers in the network. There is an edge (u, v) if and only if there
is a direct communication link between the computers corresponding
to u and v.
27
Uses for Graphs
• Two-Player Game Tree: All of the possibilities in a board game like
chess can be represented in a graph. Each vertex stands for one
possible board position. (For chess, this is a very big graph!)
28
Social Media (Facebook)
29
Websites
30
Intercity Road Network
31
32

Graphs in data structure

  • 1.
    Data Structure Graphs Preston UniversityKohat Hamza Javed BCS 4th 2121-115001
  • 2.
  • 3.
    Introduction • Graphs area generalization of trees • Nodes or vertices • Edges or arcs • Two kinds of graphs • Directed • Undirected 3
  • 4.
    Introduction: Formal Definition •A graph G = (V,E) consists of a finite set of vertices, V, and a finite set of edges E. • Each edge is a pair (v,w) where v, w  V 4
  • 5.
    Introduction: Formal Definition •A directed graph, or digraph, is a graph in which the edges are ordered pairs • (v, w) ≠ (w, v) • An undirected graph is a graph in which the edges are unordered pairs • (v, w) == (w, v) 5
  • 6.
    Introduction: Directed Graphs •In a directed graph, the edges are arrows. • Directed graphs show the flow from one node to another and not vise versa. 6
  • 7.
    Introduction: Undirected Graphs •In a Undirected graph, the edges are lines. • UnDirected graphs show a relationship between two nodes. 7
  • 8.
    Terminology • In thedirected graph above, b is adjacent to a because (a, b)  E. Note that a is not adjacent to b. • A is a predecessor of node B • B is a successor of node A • The source of the edge is node A, the target is node B 8
  • 9.
    Terminology • In theundirected graph above, a and b are adjacent because (a,b)  E. a and b are called neighbors. 9
  • 10.
    Terminology • A pathis a sequence of vertices w1, w2,…wn such that (wi, wi+1)  E, 1 <= i < n, and each vertex is unique except that the path may start and end on the same vertex • The length of the path is the number of edges along the path 10
  • 11.
    Terminology • An acyclicpath is a path which does not follow a sequence. • A cyclic path is a path such that • There are at least two vertices on the path • w1 = wn (path starts and ends at same vertex) • And also maintains the sequence 11
  • 12.
  • 13.
    Terminology • A directedgraph that has no cyclic paths is called a DAG (a Directed Acyclic Graph). • An undirected graph that has an edge between every pair of vertices is called a complete graph. Note: A directed graph can also be a complete graph; in that case, there must be an edge from every vertex to every other vertex. 13
  • 14.
    Test Your Knowledge Complete,or “Acomplete” (Not Complete) 14
  • 15.
    Test Your Knowledge Complete,or “Acomplete” (Not Complete) 15
  • 16.
    Terminology • 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 16
  • 17.
    Terminology • A graphis known as a weighted graph if a weight or metric is associated with each edge. 17
  • 18.
  • 19.
    Following are therepresentation of a graph • Adjacency Matrix • Adjacency List There are other representations also like, Incidence Matrix and Incidence List. The choice of the graph representation is situation specific. It totally depends on the type of operations to be performed and ease of use. 19
  • 20.
    Adjacency Matrix • AdjacencyMatrix is a 2D array of size V x V where V is the number of vertices in a graph. Let the 2D array be adj[][], a slot adj[i][j] = 1 indicates that there is an edge from vertex i to vertex j. Adjacency matrix for undirected graph is always symmetric. Adjacency Matrix is also used to represent weighted graphs. If adj[i][j] = w, then there is an edge from vertex i to vertex j with weight w. 20
  • 21.
  • 22.
    Adjacency List • Anarray of linked lists is used. Size of the array is equal to number of vertices. Let the array be array[]. An entry array[i] represents the linked list of vertices adjacent to the ith vertex. This representation can also be used to represent a weighted graph. The weights of edges can be stored in nodes of linked lists. Following is adjacency list representation of the above graph. 22
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
    Uses for Graphs •Computer network: The set of vertices V represents the set of computers in the network. There is an edge (u, v) if and only if there is a direct communication link between the computers corresponding to u and v. 27
  • 28.
    Uses for Graphs •Two-Player Game Tree: All of the possibilities in a board game like chess can be represented in a graph. Each vertex stands for one possible board position. (For chess, this is a very big graph!) 28
  • 29.
  • 30.
  • 31.
  • 32.