SlideShare a Scribd company logo
1 of 59
GRAPHS
LEARNING OBJECTIVES
 After completing this chapter, you will be able to:
1. Represent the graph using array and Linked list
2. Traverse the graph
3. Calculate minimum cost spanning tree
4. Calculate the shortest route from source to all
other nodes
GRAPHS
 Graph is a collection of nodes or vertices connected
together through edges or arcs.
 It is representation of a set of objects where some
pairs of objects are connected by links. The
interconnected objects are represented by points
termed as vertices, and the links that connect the
vertices are called edges.
 Formally, a graph is a pair of sets V, E, where V is the
set of vertices and E is the set of edges, connecting
the pairs of vertices.
 Take a look at the following graph −
CONT.…
In the above graph,
V = {a, b, c, d, e}
E = {ab, ac, bd, cd, de}
USES OF GRAPHS
 Graphs are used to model electrical circuits,
chemical compounds, highway maps, and so on.
 They are also used in the analysis of electrical
circuits, finding the shortest route, project
planning, linguistics, genetics, social science, and
so forth.
GRAPH DEFINITIONS AND NOTATIONS
 A graph G is a pair, G = (V, E),
 where V is a finite nonempty set, called the set of vertices of
G. E is called the set of edges.
 Let V(G) denote the set of vertices, and E(G) denote the set
of edges of a graph G.
 If the elements of E(G) are ordered pairs, G is called a
directed graph or digraph;,
 otherwise, G is called an undirected graph. In an
undirected graph, the pairs (u, v) and (v, u) represent the
same edge.
CONT.…
 Let G be a graph.
 A graph H is called a sub-graph of G if V(H) ⊆
V(G) and E(H) ⊆ ^E(G);
 that is, every vertex of H is a vertex of G, and
every edge in H is an edge in G.
CONT.….
 A graph can be shown pictorially.
 The vertices are drawn as circles, and a label
inside the circle represents the vertex.
1. In an undirected graph, the edges are drawn using
lines.
2. In a directed graph, the edges are drawn using
arrows.
CONT.….
GRAPH DATA STRUCTURE
 Mathematical graphs can be represented in data-
structure.
 We can represent a graph using an array of
vertices and a two dimensional array of edges
IMPORTANT GRAPH’S TERMS
 Vertex − Each node of the graph is represented as a
vertex. In example given below, labeled circle represents
vertices.
 So A to E are vertices. We can represent them using an array
as shown in image below. Here A can be identified by index
0. B can be identified using index 1 and so on.
 Edge − Edge represents a path between two vertices or a
line between two vertices.
 In example given below, lines from A to B, B to D and
so on represents edges..
CONT.…..
 Adjacency − Two node or vertices are adjacent if they
are connected to each other through an edge. In
example given below, B is adjacent to A, D is adjacent to
B and so on.
 Path − Path represents a sequence of edges between
two vertices. In example given below, ABDE represents a
path from A to E.
GRAPH REPRESENTATION
 A graph can be represented in several
ways.
 Two common ways:
1. adjacency matrices and
2. adjacency lists.
ADJACENCY MATRIX
 Let G be a graph with n vertices, where n > 0.
 Let V(G) = {v1, v2, ..., vn}.
 The adjacency matrix AG is a two dimensional matrix n x
^n matrix such that the (i, j)th entry of AG is 1 if there is
an edge from vi to vj;
 otherwise, the (i,j)th entry is zero.
ADJACENCY MATRIX FOR GRAPHS
Adjacency Matrix for graphs (a) and (b)
ADJACENCY LISTS
 An Adjacency List is a linked list of Vertices adjacent to a given
vertex
 Let G be a graph with n vertices, where n > 0.
 Let V(G) = {v1, v2, ..., vn}.
 In the adjacency list representation, corresponding to each vertex,
v, there is a linked list such that each node of the linked list
contains the vertex u, such that (v, u) ⊆ E(G).
 Because there are n nodes, we use an array, A, of size n, such that
A[i] is a reference variable pointing to the first node of the linked
list containing the vertices to which vi is adjacent. Each node has
two components, say vertex and link.
 The component vertex contains the index of the vertex adjacent to
vertex i
ADJACENCY LIST OF GRAPH IN EXAMPLE
OPERATIONS ON GRAPHS
 Following are the basic primary operations of a Graph
1. Add Vertex − add a vertex to a graph.
2. Add Edge − add an edge between two vertices of a graph.
3. Display Vertex − display a vertex of a graph.
4. Create the graph. That is, store the graph in computer
memory using a particular graph representation.
5. Clear the graph. This operation makes the graph empty.
6. Determine whether the graph is empty.
7. Traverse the graph.
GRAPH TRAVERSALS
 Processing a graph requires the ability to traverse the
graph. Traversing a graph is similar to traversing a binary
tree, except that traversing a graph is a bit more
complicated. Recall that a binary tree has no cycles.
 Also, starting at the root node, we can traverse the entire
tree.
 On the other hand, a graph might have cycles and we
might not be able to traverse the entire graph from a single
vertex (for example, if the graph is not connected).
 Therefore, we must keep track of the vertices that have
been visited. We must also traverse the graph from each
vertex (that has not been visited) of the graph.
CONT.….
 This ensures that the entire graph is traversed.
 The two most common graph traversal
algorithms are:
1. Depth first traversal
2. Breadth first traversal
DEPTH FIRST TRAVERSAL
 Depth First Search algorithm DFS traverses a graph in a depth
ward motion and uses a stack to remember to get the next
vertex to start a search when a dead end occurs in any
iteration.
 Depth First Search- a search that begins by visiting vertex V,
an then recursively searches the unvisited vertices adjacent to V.
 The depth first traversal is similar to the preorder traversal of a
binary tree.
 An initial or source vertex is identified to start traversing, then
from that vertex any one vertex which is adjacent to the current
vertex is traversed i.e. only one adjacent vertex is traversed
from the vertex which had been traversed last.
CONT.….
DEPTH FIRST TRAVERSAL
As in example given above, DFS algorithm traverses from A to B
to C to D first then to E, then to F and lastly to G.
CONT…
 It employs following rules.
 Rule 1 − Visit adjacent unvisited vertex. Mark it visited.
Display it. Push it in a stack.
 Rule 2 − If no adjacent vertex found, pop up a vertex
from stack.
 Rule 3 − Repeat Rule 1 and Rule 2 until stack is empty.
CONT.….
CONT….
CONT.….
CONT.….
CONT.….
CONT.….
CONT.…..
As C does not have any unvisited adjacent node so we keep popping the
stack until we find a node which has unvisited adjacent node. In this
case, there's none and we keep popping until stack is empty.
BREADTH FIRST TRAVERSAL
 Breadth First Search algorithm BFS traverses a graph in a
breadth wards motion and uses a queue to remember to
get the next vertex to start a search when a dead end
occurs in any iteration.
 Breadth-First search- A search that begins by visiting
vertex V, then visits the vertices adjacent to V, then visit the
vertices adjacent to each of V’s adjacent vertices, and so
on.
 The breadth first traversal of a graph is similar to traversing
a binary tree level by level (the nodes at each level are
visited from left to right).All the nodes at any level, i, are
visited before visiting the nodes at level i + 1.
CONT….
CONT.…
As in example given above, BFS algorithm traverses from A to B to E
to F first then to C and G lastly to D.
CONT…
 It employs following rules.
 Rule 1 − Visit adjacent unvisited vertex. Mark it visited.
Display it. Insert it in a queue.
 Rule 2 − If no adjacent vertex found, remove the first
vertex from queue.
 Rule 3 − Repeat Rule 1 and Rule 2 until queue is empty.
CONT.….
CONT.….
CONT…
CONT.…
CONT.…
CONT.…..
CONT.…..
At this stage we are left with no unmarked unvisited nodes. But as per
algorithm we keep on dequeuing in order to get all unvisited nodes.
When the queue gets emptied the program is over.
SHORTEST PATH ALGORITHM
 Shortest path can be calculated only for the weighted
graphs.
 The edges connecting two vertices can be assigned a
nonnegative real number, called the weight of the edge.
 A graph with such weighted edges is called a weighted
graph.
 Let G be a weighted graph. Let u and v be two vertices in
G, and let P be a path in G from u to v.
 The weight of the path P is the sum of the weights of all
the edges on the path P, which is also called the weight of
v from u via P.
CONT.….
 Let G be a weighted graph representing a highway
structure. Suppose that the weight of an edge represents
the travel time.
 For example, to plan monthly business trips, a
salesperson wants to find the shortest path (that is, the
path with the smallest weight) from her or his city to
every other city in the graph. Many such problems exist in
which we want to find the shortest path from a given
vertex, called the source, to every other vertex in the
graph.
 This section describes the shortest path algorithm, also
called the greedy algorithm, developed by Dijkstra.
SHORTEST PATH
 Given a vertex, say vertex (that is, a source), this section
describes the shortest path algorithm.
 The general algorithm is:
1. Initialize the array smallest Weight so that smallest
Weight[u]=weights[vertex, u].
2. Set smallest Weight[vertex] = 0.
3. Find the vertex, v, that is closest to vertex for which the shortest path has
not been determined.
4. Mark v as the (next) vertex for which the smallest weight is found.
5. For each vertex w in G, such that the shortest path from vertex to w has
not been determined and an edge (v, w) exists, if the weight of the path
to w via v is smaller than its current weight, update the weight of w to the
weight of v + the weight of the edge (v, w).
CONT…..
CONT.….
CONT……
 Therefore A-B-D (3) < A-D (5)
 Adjusted from B
 Select A-C
MINIMUM SPANNING TREES
 A spanning tree of a graph, G, is a set of |V|-1 edges that connect all vertices
of the graph.
 Suppose we have a group of islands that we wish to link with bridges so that
it is possible to travel from one island to any other in the group.
 Further suppose that (as usual) our government wishes to spend the
absolute minimum amount on this project (because other factors like the
cost of using, maintaining, etc., these bridges will probably be the
responsibility of some future government). The engineers are able to
produce a cost for a bridge linking each possible pair of islands. The set of
bridges which will enable one to travel from any island to any other at
minimum capital cost to the government is the minimum spanning tree.
 In general, it is possible to construct multiple spanning trees for a graph, G.
If a cost, cij, is associated with each edge, eij = (vi,vj), then the minimum
spanning tree is the set of edges, E span, forming a spanning tree, such that:
 C = sum( cij | all eij in Espan ) is a minimum.
KRUSKAL'S ALGORITHM
 This algorithm creates a forest of trees. Initially the
forest consists of n single node trees (and no edges). At
each step, we add one (the cheapest one) edge so that
it joins two trees together.
 If it were to form a cycle, it would simply link two
nodes that were already part of a single connected tree,
so that this edge would not be needed.
CONT.….
CONT.…
 The following sequence of diagrams illustrates Kruskal's algorithm
in operation.
 Kruskal’s Algorithm
CONT.….
CONT.….
CONT.…
CONT.….
SUMMARY
 Graph is a collection of nodes Connected together using
edges.
 Graph can be traversed using DFS or BFS
 Shortest path for a vertex with other vertices can be
calculated using Dijkstra’s algorithm.
 Spanning tree is an acyclic graph.
 Minimum cost spanning trees can be derived using
Kruskal’s algorithm
TEST YOUR UNDERSTANDING
DATA STRUCTURES.pptx

More Related Content

Similar to DATA STRUCTURES.pptx

Slides Chapter10.1 10.2
Slides Chapter10.1 10.2Slides Chapter10.1 10.2
Slides Chapter10.1 10.2
showslidedump
 
Graph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptxGraph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptx
asimshahzad8611
 
Graph Theory 117 © David Lippman Creative Commons BY-
  Graph Theory   117 © David Lippman  Creative Commons BY-  Graph Theory   117 © David Lippman  Creative Commons BY-
Graph Theory 117 © David Lippman Creative Commons BY-
maple8qvlisbey
 
Graph Theory 117 © David Lippman Creative Commons BY-.docx
Graph Theory   117 © David Lippman  Creative Commons BY-.docxGraph Theory   117 © David Lippman  Creative Commons BY-.docx
Graph Theory 117 © David Lippman Creative Commons BY-.docx
durantheseldine
 
Mithfh lecturenotes 9
Mithfh lecturenotes 9Mithfh lecturenotes 9
Mithfh lecturenotes 9
Praveen Kumar
 

Similar to DATA STRUCTURES.pptx (20)

UNIT III.pptx
UNIT III.pptxUNIT III.pptx
UNIT III.pptx
 
Data structure note
Data structure noteData structure note
Data structure note
 
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithm
 
Slides Chapter10.1 10.2
Slides Chapter10.1 10.2Slides Chapter10.1 10.2
Slides Chapter10.1 10.2
 
Graph_data_structure_information_engineering.pptx
Graph_data_structure_information_engineering.pptxGraph_data_structure_information_engineering.pptx
Graph_data_structure_information_engineering.pptx
 
Graph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptxGraph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptx
 
VANU no sql ppt.pptx
VANU no sql ppt.pptxVANU no sql ppt.pptx
VANU no sql ppt.pptx
 
Vanmathy no sql
Vanmathy no sql Vanmathy no sql
Vanmathy no sql
 
Graph
GraphGraph
Graph
 
Spanningtreesppt
SpanningtreespptSpanningtreesppt
Spanningtreesppt
 
Graph Theory 117 © David Lippman Creative Commons BY-
  Graph Theory   117 © David Lippman  Creative Commons BY-  Graph Theory   117 © David Lippman  Creative Commons BY-
Graph Theory 117 © David Lippman Creative Commons BY-
 
Graph Theory 117 © David Lippman Creative Commons BY-.docx
Graph Theory   117 © David Lippman  Creative Commons BY-.docxGraph Theory   117 © David Lippman  Creative Commons BY-.docx
Graph Theory 117 © David Lippman Creative Commons BY-.docx
 
data structures and algorithms Unit 2
data structures and algorithms Unit 2data structures and algorithms Unit 2
data structures and algorithms Unit 2
 
graph representation.pdf
graph representation.pdfgraph representation.pdf
graph representation.pdf
 
Graph theory concepts complex networks presents-rouhollah nabati
Graph theory concepts   complex networks presents-rouhollah nabatiGraph theory concepts   complex networks presents-rouhollah nabati
Graph theory concepts complex networks presents-rouhollah nabati
 
graph ASS (1).ppt
graph ASS (1).pptgraph ASS (1).ppt
graph ASS (1).ppt
 
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringGraph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
 
Graph ASS DBATU.pptx
Graph ASS DBATU.pptxGraph ASS DBATU.pptx
Graph ASS DBATU.pptx
 
Graphs.pptx
Graphs.pptxGraphs.pptx
Graphs.pptx
 
Mithfh lecturenotes 9
Mithfh lecturenotes 9Mithfh lecturenotes 9
Mithfh lecturenotes 9
 

Recently uploaded

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 

Recently uploaded (20)

Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 

DATA STRUCTURES.pptx

  • 2. LEARNING OBJECTIVES  After completing this chapter, you will be able to: 1. Represent the graph using array and Linked list 2. Traverse the graph 3. Calculate minimum cost spanning tree 4. Calculate the shortest route from source to all other nodes
  • 3. GRAPHS  Graph is a collection of nodes or vertices connected together through edges or arcs.  It is representation of a set of objects where some pairs of objects are connected by links. The interconnected objects are represented by points termed as vertices, and the links that connect the vertices are called edges.  Formally, a graph is a pair of sets V, E, where V is the set of vertices and E is the set of edges, connecting the pairs of vertices.  Take a look at the following graph −
  • 4. CONT.… In the above graph, V = {a, b, c, d, e} E = {ab, ac, bd, cd, de}
  • 5. USES OF GRAPHS  Graphs are used to model electrical circuits, chemical compounds, highway maps, and so on.  They are also used in the analysis of electrical circuits, finding the shortest route, project planning, linguistics, genetics, social science, and so forth.
  • 6. GRAPH DEFINITIONS AND NOTATIONS  A graph G is a pair, G = (V, E),  where V is a finite nonempty set, called the set of vertices of G. E is called the set of edges.  Let V(G) denote the set of vertices, and E(G) denote the set of edges of a graph G.  If the elements of E(G) are ordered pairs, G is called a directed graph or digraph;,  otherwise, G is called an undirected graph. In an undirected graph, the pairs (u, v) and (v, u) represent the same edge.
  • 7. CONT.…  Let G be a graph.  A graph H is called a sub-graph of G if V(H) ⊆ V(G) and E(H) ⊆ ^E(G);  that is, every vertex of H is a vertex of G, and every edge in H is an edge in G.
  • 8. CONT.….  A graph can be shown pictorially.  The vertices are drawn as circles, and a label inside the circle represents the vertex. 1. In an undirected graph, the edges are drawn using lines. 2. In a directed graph, the edges are drawn using arrows.
  • 10. GRAPH DATA STRUCTURE  Mathematical graphs can be represented in data- structure.  We can represent a graph using an array of vertices and a two dimensional array of edges
  • 11. IMPORTANT GRAPH’S TERMS  Vertex − Each node of the graph is represented as a vertex. In example given below, labeled circle represents vertices.  So A to E are vertices. We can represent them using an array as shown in image below. Here A can be identified by index 0. B can be identified using index 1 and so on.  Edge − Edge represents a path between two vertices or a line between two vertices.  In example given below, lines from A to B, B to D and so on represents edges..
  • 12. CONT.…..  Adjacency − Two node or vertices are adjacent if they are connected to each other through an edge. In example given below, B is adjacent to A, D is adjacent to B and so on.  Path − Path represents a sequence of edges between two vertices. In example given below, ABDE represents a path from A to E.
  • 13. GRAPH REPRESENTATION  A graph can be represented in several ways.  Two common ways: 1. adjacency matrices and 2. adjacency lists.
  • 14. ADJACENCY MATRIX  Let G be a graph with n vertices, where n > 0.  Let V(G) = {v1, v2, ..., vn}.  The adjacency matrix AG is a two dimensional matrix n x ^n matrix such that the (i, j)th entry of AG is 1 if there is an edge from vi to vj;  otherwise, the (i,j)th entry is zero.
  • 15. ADJACENCY MATRIX FOR GRAPHS Adjacency Matrix for graphs (a) and (b)
  • 16. ADJACENCY LISTS  An Adjacency List is a linked list of Vertices adjacent to a given vertex  Let G be a graph with n vertices, where n > 0.  Let V(G) = {v1, v2, ..., vn}.  In the adjacency list representation, corresponding to each vertex, v, there is a linked list such that each node of the linked list contains the vertex u, such that (v, u) ⊆ E(G).  Because there are n nodes, we use an array, A, of size n, such that A[i] is a reference variable pointing to the first node of the linked list containing the vertices to which vi is adjacent. Each node has two components, say vertex and link.  The component vertex contains the index of the vertex adjacent to vertex i
  • 17. ADJACENCY LIST OF GRAPH IN EXAMPLE
  • 18. OPERATIONS ON GRAPHS  Following are the basic primary operations of a Graph 1. Add Vertex − add a vertex to a graph. 2. Add Edge − add an edge between two vertices of a graph. 3. Display Vertex − display a vertex of a graph. 4. Create the graph. That is, store the graph in computer memory using a particular graph representation. 5. Clear the graph. This operation makes the graph empty. 6. Determine whether the graph is empty. 7. Traverse the graph.
  • 19. GRAPH TRAVERSALS  Processing a graph requires the ability to traverse the graph. Traversing a graph is similar to traversing a binary tree, except that traversing a graph is a bit more complicated. Recall that a binary tree has no cycles.  Also, starting at the root node, we can traverse the entire tree.  On the other hand, a graph might have cycles and we might not be able to traverse the entire graph from a single vertex (for example, if the graph is not connected).  Therefore, we must keep track of the vertices that have been visited. We must also traverse the graph from each vertex (that has not been visited) of the graph.
  • 20. CONT.….  This ensures that the entire graph is traversed.  The two most common graph traversal algorithms are: 1. Depth first traversal 2. Breadth first traversal
  • 21. DEPTH FIRST TRAVERSAL  Depth First Search algorithm DFS traverses a graph in a depth ward motion and uses a stack to remember to get the next vertex to start a search when a dead end occurs in any iteration.  Depth First Search- a search that begins by visiting vertex V, an then recursively searches the unvisited vertices adjacent to V.  The depth first traversal is similar to the preorder traversal of a binary tree.  An initial or source vertex is identified to start traversing, then from that vertex any one vertex which is adjacent to the current vertex is traversed i.e. only one adjacent vertex is traversed from the vertex which had been traversed last.
  • 23. DEPTH FIRST TRAVERSAL As in example given above, DFS algorithm traverses from A to B to C to D first then to E, then to F and lastly to G.
  • 24. CONT…  It employs following rules.  Rule 1 − Visit adjacent unvisited vertex. Mark it visited. Display it. Push it in a stack.  Rule 2 − If no adjacent vertex found, pop up a vertex from stack.  Rule 3 − Repeat Rule 1 and Rule 2 until stack is empty.
  • 31. CONT.….. As C does not have any unvisited adjacent node so we keep popping the stack until we find a node which has unvisited adjacent node. In this case, there's none and we keep popping until stack is empty.
  • 32. BREADTH FIRST TRAVERSAL  Breadth First Search algorithm BFS traverses a graph in a breadth wards motion and uses a queue to remember to get the next vertex to start a search when a dead end occurs in any iteration.  Breadth-First search- A search that begins by visiting vertex V, then visits the vertices adjacent to V, then visit the vertices adjacent to each of V’s adjacent vertices, and so on.  The breadth first traversal of a graph is similar to traversing a binary tree level by level (the nodes at each level are visited from left to right).All the nodes at any level, i, are visited before visiting the nodes at level i + 1.
  • 34. CONT.… As in example given above, BFS algorithm traverses from A to B to E to F first then to C and G lastly to D.
  • 35. CONT…  It employs following rules.  Rule 1 − Visit adjacent unvisited vertex. Mark it visited. Display it. Insert it in a queue.  Rule 2 − If no adjacent vertex found, remove the first vertex from queue.  Rule 3 − Repeat Rule 1 and Rule 2 until queue is empty.
  • 42. CONT.….. At this stage we are left with no unmarked unvisited nodes. But as per algorithm we keep on dequeuing in order to get all unvisited nodes. When the queue gets emptied the program is over.
  • 43. SHORTEST PATH ALGORITHM  Shortest path can be calculated only for the weighted graphs.  The edges connecting two vertices can be assigned a nonnegative real number, called the weight of the edge.  A graph with such weighted edges is called a weighted graph.  Let G be a weighted graph. Let u and v be two vertices in G, and let P be a path in G from u to v.  The weight of the path P is the sum of the weights of all the edges on the path P, which is also called the weight of v from u via P.
  • 44. CONT.….  Let G be a weighted graph representing a highway structure. Suppose that the weight of an edge represents the travel time.  For example, to plan monthly business trips, a salesperson wants to find the shortest path (that is, the path with the smallest weight) from her or his city to every other city in the graph. Many such problems exist in which we want to find the shortest path from a given vertex, called the source, to every other vertex in the graph.  This section describes the shortest path algorithm, also called the greedy algorithm, developed by Dijkstra.
  • 45. SHORTEST PATH  Given a vertex, say vertex (that is, a source), this section describes the shortest path algorithm.  The general algorithm is: 1. Initialize the array smallest Weight so that smallest Weight[u]=weights[vertex, u]. 2. Set smallest Weight[vertex] = 0. 3. Find the vertex, v, that is closest to vertex for which the shortest path has not been determined. 4. Mark v as the (next) vertex for which the smallest weight is found. 5. For each vertex w in G, such that the shortest path from vertex to w has not been determined and an edge (v, w) exists, if the weight of the path to w via v is smaller than its current weight, update the weight of w to the weight of v + the weight of the edge (v, w).
  • 48. CONT……  Therefore A-B-D (3) < A-D (5)  Adjusted from B  Select A-C
  • 49. MINIMUM SPANNING TREES  A spanning tree of a graph, G, is a set of |V|-1 edges that connect all vertices of the graph.  Suppose we have a group of islands that we wish to link with bridges so that it is possible to travel from one island to any other in the group.  Further suppose that (as usual) our government wishes to spend the absolute minimum amount on this project (because other factors like the cost of using, maintaining, etc., these bridges will probably be the responsibility of some future government). The engineers are able to produce a cost for a bridge linking each possible pair of islands. The set of bridges which will enable one to travel from any island to any other at minimum capital cost to the government is the minimum spanning tree.  In general, it is possible to construct multiple spanning trees for a graph, G. If a cost, cij, is associated with each edge, eij = (vi,vj), then the minimum spanning tree is the set of edges, E span, forming a spanning tree, such that:  C = sum( cij | all eij in Espan ) is a minimum.
  • 50. KRUSKAL'S ALGORITHM  This algorithm creates a forest of trees. Initially the forest consists of n single node trees (and no edges). At each step, we add one (the cheapest one) edge so that it joins two trees together.  If it were to form a cycle, it would simply link two nodes that were already part of a single connected tree, so that this edge would not be needed.
  • 52. CONT.…  The following sequence of diagrams illustrates Kruskal's algorithm in operation.  Kruskal’s Algorithm
  • 57. SUMMARY  Graph is a collection of nodes Connected together using edges.  Graph can be traversed using DFS or BFS  Shortest path for a vertex with other vertices can be calculated using Dijkstra’s algorithm.  Spanning tree is an acyclic graph.  Minimum cost spanning trees can be derived using Kruskal’s algorithm