SlideShare a Scribd company logo
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

PPTX
logic.pptx
PPTX
Graph data structures for ppt for understanding.pptx
PPT
Data Structures-Non Linear DataStructures-Graphs
PPTX
Unit ix graph
PDF
Unit-10 Graphs .pdf
PDF
Graphhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh.pdf
PPTX
Unit 9 graph
PPTX
Algorithms and data Chapter 3 V Graph.pptx
logic.pptx
Graph data structures for ppt for understanding.pptx
Data Structures-Non Linear DataStructures-Graphs
Unit ix graph
Unit-10 Graphs .pdf
Graphhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh.pdf
Unit 9 graph
Algorithms and data Chapter 3 V Graph.pptx

Similar to DATA STRUCTURES.pptx (20)

PPT
Unit VI - Graphs.ppt
PPT
Graphs Presentation of University by Coordinator
PPTX
Lecture 2.3.1 Graph.pptx
PPTX
Basic Graph Algorithms Vertex (Node): lk
PPTX
Data Structures and Agorithm: DS 21 Graph Theory.pptx
PPTX
Data Structure and algorithms - Graph1.pptx
PPTX
130210107039 2130702
PPTX
UNIT IV NON LINEAR DATA STRUCTURES - GRAPH
PPT
Graphs
PPT
Graphs.pptGraphs.pptGraphs.pptGraphs.pptGraphs.pptGraphs.ppt
PPTX
UNIT III.pptx
PDF
Daa chpater 12
PPTX
UNIT IV NON LINEAR DATA STRUCTURES - GRAPH.pptx
PPTX
Unit 4 dsuc
PPT
Lecture 5b graphs and hashing
PPT
14. GRAPH in data structures and algorithm.ppt
PDF
LEC 12-DSALGO-GRAPHS(final12).pdf
PPTX
Graph in data structure
Unit VI - Graphs.ppt
Graphs Presentation of University by Coordinator
Lecture 2.3.1 Graph.pptx
Basic Graph Algorithms Vertex (Node): lk
Data Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structure and algorithms - Graph1.pptx
130210107039 2130702
UNIT IV NON LINEAR DATA STRUCTURES - GRAPH
Graphs
Graphs.pptGraphs.pptGraphs.pptGraphs.pptGraphs.pptGraphs.ppt
UNIT III.pptx
Daa chpater 12
UNIT IV NON LINEAR DATA STRUCTURES - GRAPH.pptx
Unit 4 dsuc
Lecture 5b graphs and hashing
14. GRAPH in data structures and algorithm.ppt
LEC 12-DSALGO-GRAPHS(final12).pdf
Graph in data structure
Ad

Recently uploaded (20)

PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Business Ethics Teaching Materials for college
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Classroom Observation Tools for Teachers
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PPTX
Cell Types and Its function , kingdom of life
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Supply Chain Operations Speaking Notes -ICLT Program
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Business Ethics Teaching Materials for college
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Anesthesia in Laparoscopic Surgery in India
Week 4 Term 3 Study Techniques revisited.pptx
Pharma ospi slides which help in ospi learning
Pharmacology of Heart Failure /Pharmacotherapy of CHF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Renaissance Architecture: A Journey from Faith to Humanism
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Classroom Observation Tools for Teachers
Module 4: Burden of Disease Tutorial Slides S2 2025
102 student loan defaulters named and shamed – Is someone you know on the list?
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Cell Types and Its function , kingdom of life
VCE English Exam - Section C Student Revision Booklet
Microbial disease of the cardiovascular and lymphatic systems
Supply Chain Operations Speaking Notes -ICLT Program
Ad

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