UNIT-IV
GRAPH
Basic Concepts
 a) Graph
 A Graph G(V, E) consists of two sets
 A set V, called set of all vertices or nodes
 A set E, called set of all edges.
 An edge e = (v, w) is a pair of vertices v and w and is said to be
path between v and w. Figure 1 shows a graph for which there
are five vertices and five edges.
 V = {v1, v2, v3, v4, v5}
 and E = {e1, e2, e3, e4, e5}
 Directed Graph
 Degree of Vertex: The number of edges connected
with vertex v is called the degree of vertex v, and
is denoted by degree(v). But for a digraph, there
are two degrees: indegree and outdegree
 Acyclic Graph
 Adjacent Vertices
 Self loop
 Parallel Edges
 Simple Graph (digraph)
 Complete Graph: A graph (digraph) is said to be complete
if each vertex vi, is adjacent to every other vertex vj in a
graph, In other words, there are edges from any vertex to
all other ver
 Isolated Vertex: A vertex is isolated if there is no edge
connected from any other vertex to the vertex.
 Weighted Graph: A graph (or digraph) is termed as
weighted graph if all the edges in it are labeled with some
weights
 Connected Graph: In a graph (not digraph) G, two vertices
vi and vj are said to be connected if there is a path in G
from vi to vj (or vi to vj)
Representation of Graphs in Memory
 Adjacency Matrix
 Matrix representation is the most useful way of representing any
graph. This representation uses a square matrix of order n  n, n
being the number of vertices in the graph. Entries in the matrix
can be decided as follows:
 This matrix is known as adjacency because n entry stores the
information whether two vertices are adjacent or not. Also the
matrix is alternatively termed as bit matrix o Boolean matrix as
the entries are either 0 or 1.
 Adjacency matrix is also useful to store multigraph and weighted
graph. In case of multigraph representation instead of entry 1, the
entry will be the number of edges between two vertices. And is
case of weighted graph, the entries are weighted of the edges
between the vertices instead of 0 or 1
Drawback of Adjacency Matrix
Representation
 Difficult to insert and delete nodes in G. This is
because the size of A may need to be changed
and the nodes may need to be reordered, so
there may be many changes in the matrix A.
 If the number of edges is O(m) or O(m log2 m),
then the matrix A will be sparse; hence a large
space will be wasted
Path Matrix
Finding Path Matrix
Linked Representation of a Graph
 The linked representation will contain two lists:
 A node list NODE and
 A edge list EDGE
Shortest Path Problem
 Shortest path problem of a graph is about finding
a path between two vertices in such a way that
this path will satisfy some criteria of
optimization. For example, for a non-weighted
graph, the number of edges will be minimum
and for weighted graph, the sum of weights on
all edges in the path will be minimum. A few
important algorithms are Warshall's algorithm,
Floyd's algorithm and Dijkstra's algorithm.
Warshall's algorithm
Traversing a Graph
 Depth-First Search(DFS)
 DFS traversal is similar to the inorder traversal
of a tree. It begins at vertex v, then visit the
vertex v first. Then visit all the vertices along a
path which begins at v. This means that, visit the
vertex v then the vertex immediate adjacent to v,
let it be vx. Next, if vx has an immediate adjacent
vy, then visit it and so on, till there is a 'dead end'.
This results a path P, v – vx – vy.
Breadth-First Search(BFS)
UNIT-VIQueueQueueQueueQueueQueueQueueQueueQueue.pptx

UNIT-VIQueueQueueQueueQueueQueueQueueQueueQueue.pptx

  • 1.
  • 2.
    Basic Concepts  a)Graph  A Graph G(V, E) consists of two sets  A set V, called set of all vertices or nodes  A set E, called set of all edges.  An edge e = (v, w) is a pair of vertices v and w and is said to be path between v and w. Figure 1 shows a graph for which there are five vertices and five edges.  V = {v1, v2, v3, v4, v5}  and E = {e1, e2, e3, e4, e5}
  • 3.
     Directed Graph Degree of Vertex: The number of edges connected with vertex v is called the degree of vertex v, and is denoted by degree(v). But for a digraph, there are two degrees: indegree and outdegree  Acyclic Graph  Adjacent Vertices  Self loop  Parallel Edges  Simple Graph (digraph)
  • 4.
     Complete Graph:A graph (digraph) is said to be complete if each vertex vi, is adjacent to every other vertex vj in a graph, In other words, there are edges from any vertex to all other ver  Isolated Vertex: A vertex is isolated if there is no edge connected from any other vertex to the vertex.  Weighted Graph: A graph (or digraph) is termed as weighted graph if all the edges in it are labeled with some weights  Connected Graph: In a graph (not digraph) G, two vertices vi and vj are said to be connected if there is a path in G from vi to vj (or vi to vj)
  • 5.
    Representation of Graphsin Memory  Adjacency Matrix  Matrix representation is the most useful way of representing any graph. This representation uses a square matrix of order n  n, n being the number of vertices in the graph. Entries in the matrix can be decided as follows:  This matrix is known as adjacency because n entry stores the information whether two vertices are adjacent or not. Also the matrix is alternatively termed as bit matrix o Boolean matrix as the entries are either 0 or 1.  Adjacency matrix is also useful to store multigraph and weighted graph. In case of multigraph representation instead of entry 1, the entry will be the number of edges between two vertices. And is case of weighted graph, the entries are weighted of the edges between the vertices instead of 0 or 1
  • 6.
    Drawback of AdjacencyMatrix Representation  Difficult to insert and delete nodes in G. This is because the size of A may need to be changed and the nodes may need to be reordered, so there may be many changes in the matrix A.  If the number of edges is O(m) or O(m log2 m), then the matrix A will be sparse; hence a large space will be wasted
  • 9.
  • 10.
  • 11.
    Linked Representation ofa Graph  The linked representation will contain two lists:  A node list NODE and  A edge list EDGE
  • 15.
    Shortest Path Problem Shortest path problem of a graph is about finding a path between two vertices in such a way that this path will satisfy some criteria of optimization. For example, for a non-weighted graph, the number of edges will be minimum and for weighted graph, the sum of weights on all edges in the path will be minimum. A few important algorithms are Warshall's algorithm, Floyd's algorithm and Dijkstra's algorithm.
  • 16.
  • 21.
    Traversing a Graph Depth-First Search(DFS)  DFS traversal is similar to the inorder traversal of a tree. It begins at vertex v, then visit the vertex v first. Then visit all the vertices along a path which begins at v. This means that, visit the vertex v then the vertex immediate adjacent to v, let it be vx. Next, if vx has an immediate adjacent vy, then visit it and so on, till there is a 'dead end'. This results a path P, v – vx – vy.
  • 24.