SlideShare a Scribd company logo
1 of 82
GRAPHS
• Graph is a non-linear data structure. It
contains a set of points known as nodes (or
vertices) and a set of links known as edges (or
Arcs). Here edges are used to connect the
vertices.
• Graph is a collection of vertices and arcs in
which vertices are connected with arcs
Contd…
• Graph is a collection of nodes and edges in
which nodes are connected with edges
• Generally, a graph G is represented as G = ( V ,
E ), where V is set of vertices and E is set of
edges.
Example
• The following is a graph with 5 vertices and 6
edges.
This graph G can be defined as G = ( V , E )
Where V = {A,B,C,D,E} and E =
{(A,B),(A,C)(A,D),(B,D),(C,D),(B,E),(E,D)}.
Contd…
Graph Terminology
Vertex
Individual data element of a graph is called as
Vertex. Vertex is also known as node. In above
example graph, A, B, C, D & E are known as
vertices.
Edge
• An edge is a connecting link between two vertices. Edge is
also known as Arc. An edge is represented as
(startingVertex, endingVertex). For example, in above graph
the link between vertices A and B is represented as (A,B). In
above example graph, there are 7 edges (i.e., (A,B), (A,C),
(A,D), (B,D), (B,E), (C,D), (D,E)).
Edges are three types.
• Undirected Edge - An undirected egde is a bidirectional
edge. If there is undirected edge between vertices A and B
then edge (A , B) is equal to edge (B , A).
• Directed Edge - A directed egde is a unidirectional edge. If
there is directed edge between vertices A and B then edge
(A , B) is not equal to edge (B , A).
• Weighted Edge - A weighted egde is a edge with value
(cost) on it.
• Undirected Graph
A graph with only undirected edges is said to
be undirected graph.
• Directed Graph
A graph with only directed edges is said to be
directed graph.
• Mixed Graph
A graph with both undirected and directed
edges is said to be mixed graph.
End vertices or Endpoints
• The two vertices joined by edge are called end
vertices (or endpoints) of that edge
Origin
• If a edge is directed, its first endpoint is said to
be the origin of it.
Destination
• If a edge is directed, its first endpoint is said to
be the origin of it and the other endpoint is
said to be the destination of that edge.
Adjacent
• If there is an edge between vertices A and B
then both A and B are said to be adjacent. In
other words, vertices A and B are said to be
adjacent if there is an edge between them.
Incident
• Edge is said to be incident on a vertex if the
vertex is one of the endpoints of that edge.
Outgoing Edge
• A directed edge is said to be outgoing edge on
its origin vertex.
Incoming Edge
• A directed edge is said to be incoming edge on
its destination vertex.
Degree
• Total number of edges connected to a vertex is
said to be degree of that vertex.
Indegree
• Total number of incoming edges connected to
a vertex is said to be indegree of that vertex.
Outdegree
• Total number of outgoing edges connected to
a vertex is said to be outdegree of that vertex.
Parallel edges or Multiple edges
• If there are two undirected edges with same
end vertices and two directed edges with
same origin and destination, such edges are
called parallel edges or multiple edges.
Self-loop
• Edge (undirected or directed) is a self-loop if
its two endpoints coincide with each other.
Simple Graph
• A graph is said to be simple if there are no
parallel and self-loop edges.
Path
• A path is a sequence of alternate vertices and
edges that starts at a vertex and ends at other
vertex such that each edge is incident to its
predecessor and successor vertex.
Graph Representations
Graph data structure is represented using
following representations...
• Adjacency Matrix
• Incidence Matrix
• Adjacency List
Adjacency Matrix
• In this representation, the graph is represented using a
matrix of size total number of vertices by a total
number of vertices. That means a graph with 4 vertices
is represented using a matrix of size 4X4. In this matrix,
both rows and columns represent vertices. This matrix
is filled with either 1 or 0. Here, 1 represents that there
is an edge from row vertex to column vertex and 0
represents that there is no edge from row vertex to
column vertex.
For example, consider the following undirected graph
representation...
Contd…
Directed graph representation...
Incidence Matrix
• In this representation, the graph is represented using a
matrix of size total number of vertices by a total number of
edges. That means graph with 4 vertices and 6 edges is
represented using a matrix of size 4X6. In this matrix, rows
represent vertices and columns represents edges. This
matrix is filled with 0 or 1 or -1. Here, 0 represents that the
row edge is not connected to column vertex, 1 represents
that the row edge is connected as the outgoing edge to
column vertex and -1 represents that the row edge is
connected as the incoming edge to column vertex.
For example, consider the following directed graph
representation...
Contd…
Adjacency List
• In this representation, every vertex of a graph
contains list of its adjacent vertices.
For example, consider the following directed
graph representation implemented using
linked list...
Contd..
This representation can also be
implemented using an array as
follows..
Graph Traversal
• Graph traversal is a technique used for a searching
vertex in a graph. The graph traversal is also used to
decide the order of vertices is visited in the search
process. A graph traversal finds the edges to be used in
the search process without creating loops. That means
using graph traversal we visit all the vertices of the
graph without getting into looping path.
There are two graph traversal techniques and they are
as follows...
• DFS (Depth First Search)
• BFS (Breadth First Search)
DFS (Depth First Search)
• DFS traversal of a graph produces a spanning
tree as final result. Spanning Tree is a graph
without loops. We use Stack data
structure with maximum size of total number
of vertices in the graph to implement DFS
traversal.
We use the following steps to implement DFS
traversal...
Algorithm
• Step 1 - Define a Stack of size total number of vertices in the
graph.
• Step 2 - Select any vertex as starting point for traversal. Visit
that vertex and push it on to the Stack.
• Step 3 - Visit any one of the non-visited adjacent vertices of a
vertex which is at the top of stack and push it on to the stack.
• Step 4 - Repeat step 3 until there is no new vertex to be
visited from the vertex which is at the top of the stack.
• Step 5 - When there is no new vertex to visit then use back
tracking and pop one vertex from the stack.
• Step 6 - Repeat steps 3, 4 and 5 until stack becomes Empty.
• Step 7 - When stack becomes Empty, then produce final
spanning tree by removing unused edges from the graph
Back tracking is coming back to the
vertex from which we reached the
current vertex.
BFS (Breadth First Search)
• BFS traversal of a graph produces a spanning
tree as final result. Spanning Tree is a graph
without loops. We use Queue data
structure with maximum size of total number
of vertices in the graph to implement BFS
traversal.
Algorithm
Step 1 - Define a Queue of size total number of vertices in the
graph.
Step 2 - Select any vertex as starting point for traversal. Visit
that vertex and insert it into the Queue.
Step 3 - Visit all the non-visited adjacent vertices of the vertex
which is at front of the Queue and insert them into the
Queue.
Step 4 - When there is no new vertex to be visited from the
vertex which is at front of the Queue then delete that
vertex.
Step 5 - Repeat steps 3 and 4 until queue becomes empty.
Step 6 - When queue becomes empty, then produce final
spanning tree by removing unused edges from the graph
BFS EXAMPLE
BFS EXAMPLE
TOPOLOGICAL SORTING
• Topological Sorting is possible if and only if the
graph is a Directed Acyclic Graph.
• There may exist multiple different topological
orderings for a given directed acyclic graph.
Applications of Topological Sort-
• Scheduling jobs from the given dependencies
among jobs
• Instruction Scheduling
• Determining the order of compilation tasks to
perform in makefiles
• Data Serialization
EXAMPLE
STEPS
• Write in-degree of each vertex-
STEP 2
• Vertex-A has the least in-degree.
• So, remove vertex-A and its associated edges.
• Now, update the in-degree of other vertices.
STEP 2
STEP 3
• Vertex-A has the least in-degree.
• So, remove vertex-A and its associated edges.
• Now, update the in-degree of other vertices.
STEP 4
• There are two vertices with the least in-
degree. So, following 2 cases are possible-
• In case-01,
• Remove vertex-C and its associated edges.
• Then, update the in-degree of other vertices.
• In case-02,
• Remove vertex-D and its associated edges.
• Then, update the in-degree of other vertices.
STEP 4
STEP 5
• Now, the above two cases are continued
separately in the similar manner.
• In case-01,
• Remove vertex-D since it has the least in-degree.
• Then, remove the remaining vertex-E.
• In case-02,
• Remove vertex-C since it has the least in-degree.
• Then, remove the remaining vertex-E.
STEP 5
CONCLUSION
• For the given graph, following 2 different
topological orderings are possible-
• A B C D E
• A B D C E
https://www.cs.usfca.edu/~galles/visualization/
TopoSortIndegree.html
https://www.gatevidyalay.com/topological-sort-
topological-sorting/
Dijsktra Algorithm
• Dijkstra Algorithm is a very famous greedy
algorithm.
• It is used for solving the single source shortest
path problem.
• It computes the shortest path from one
particular source node to all other remaining
nodes of the graph.
Conditions
• It is important to note the following points regarding
Dijkstra Algorithm-
• Dijkstra algorithm works only for connected graphs.
• Dijkstra algorithm works only for those graphs that do not
contain any negative weight edge.
• The actual Dijkstra algorithm does not output the shortest
paths.
• It only provides the value or cost of the shortest paths.
• By making minor modifications in the actual algorithm, the
shortest paths can be easily obtained.
• Dijkstra algorithm works for directed as well as undirected
graphs.
Step-01
• In the first step. two sets are defined-
• One set contains all those vertices which have
been included in the shortest path tree.
• In the beginning, this set is empty.
• Other set contains all those vertices which are
still left to be included in the shortest path
tree.
• In the beginning, this set contains all the
vertices of the given graph.
Step-02:
• For each vertex of the given graph, two variables are defined as-
• Π[v] which denotes the predecessor of vertex ‘v’
• d[v] which denotes the shortest path estimate of vertex ‘v’ from the
source vertex.
• Initially, the value of these variables is set as-
• The value of variable ‘Π’ for each vertex is set to NIL i.e. Π[v] = NIL
• The value of variable ‘d’ for source vertex is set to 0 i.e. d[S] = 0
• The value of variable ‘d’ for remaining vertices is set to ∞ i.e. d[v] =
∞
Step-03:
• The following procedure is repeated until all
the vertices of the graph are processed-
• Among unprocessed vertices, a vertex with
minimum value of variable ‘d’ is chosen.
• Its outgoing edges are relaxed.
• After relaxing the edges for that vertex, the
sets created in step-01 are updated.
What is Edge Relaxation?
• Consider the edge (a,b) in the following graph-
• Here, d[a] and d[b] denotes the shortest path estimate
for vertices a and b respectively from the source vertex
‘S’.
• Now,
• If d[a] + w < d[b]
• then d[b] = d[a] + w and Π[b] = a
• This is called as edge relaxation.
EXAMPLE
STEP 1
• The following two sets are created-
• Unvisited set : {S , a , b , c , d , e}
• Visited set : { }
STEP 2
• The two variables Π and d are created for
each vertex and initialized as-
• Π[S] = Π[a] = Π[b] = Π[c] = Π[d] = Π[e] = NIL
• d[S] = 0
• d[a] = d[b] = d[c] = d[d] = d[e] = ∞
STEP 3
• Vertex ‘S’ is chosen.
• This is because shortest path estimate for
vertex ‘S’ is least.
• The outgoing edges of vertex ‘S’ are relaxed.
Before Edge Relaxation
• Now,
• d[S] + 1 = 0 + 1 = 1 < ∞
• ∴ d[a] = 1 and Π[a] = S
• d[S] + 5 = 0 + 5 = 5 < ∞
• ∴ d[b] = 5 and Π[b] = S
After Edge Relaxation
• After edge relaxation, our shortest path tree
is-
• Now, the sets are updated as-
• Unvisited set : {a , b , c , d , e}
• Visited set : {S}
Step 04
• Vertex ‘a’ is chosen.
• This is because shortest path estimate for
vertex ‘a’ is least.
• The outgoing edges of vertex ‘a’ are relaxed.
Before Edge Relaxation
• Now,
• d[a] + 2 = 1 + 2 = 3 < ∞
• ∴ d[c] = 3 and Π[c] = a
• d[a] + 1 = 1 + 1 = 2 < ∞
• ∴ d[d] = 2 and Π[d] = a
• d[b] + 2 = 1 + 2 = 3 < 5
• ∴ d[b] = 3 and Π[b] = a
EXAMPLE
After edge relaxation, our shortest
path tree is-
After edge relaxation
• Now, the sets are updated as-
• Unvisited set : {b , c , d , e}
• Visited set : {S , a}
Step-05
• Vertex ‘d’ is chosen.
• This is because shortest path estimate for
vertex ‘d’ is least.
• The outgoing edges of vertex ‘d’ are relaxed.
Before edge relaxation
• Now,
• d[d] + 2 = 2 + 2 = 4 < ∞
• ∴ d[e] = 4 and Π[e] = d
• Now, the sets are updated as-
• Unvisited set : {b , c , e}
• Visited set : {S , a , d}
After edge relaxation
Step 6
• Vertex ‘b’ is chosen.
• This is because shortest path estimate for
vertex ‘b’ is least.
• Vertex ‘c’ may also be chosen since for both
the vertices, shortest path estimate is least.
• The outgoing edges of vertex ‘b’ are relaxed.
Before Edge Relaxation
• Now,
• d[b] + 2 = 3 + 2 = 5 > 2
• ∴ No change
• After edge relaxation, our shortest path tree
remains the same as in Step-05.
• Now, the sets are updated as-
• Unvisited set : {c , e}
• Visited set : {S , a , d , b}
Step-07
• Vertex ‘c’ is chosen.
• This is because shortest path estimate for
vertex ‘c’ is least.
• The outgoing edges of vertex ‘c’ are relaxed.
Before Edge Relaxation
• Now,
• d[c] + 1 = 3 + 1 = 4 = 4
• ∴ No change
• After edge relaxation, our shortest path tree
remains the same as in Step-05.
• Now, the sets are updated as-
• Unvisited set : {e}
• Visited set : {S , a , d , b , c}
Step 8
• Vertex ‘e’ is chosen.
• This is because shortest path estimate for vertex ‘e’ is least.
• The outgoing edges of vertex ‘e’ are relaxed.
• There are no outgoing edges for vertex ‘e’.
• So, our shortest path tree remains the same as in Step-05.
• Now, the sets are updated as-
• Unvisited set : { }
• Visited set : {S , a , d , b , c , e}
Conclusion
• Now,
• All vertices of the graph are processed.
• Our final shortest path tree is as shown below.
• It represents the shortest path from source
vertex ‘S’ to all other remaining vertices.
• The order in which all the vertices are
processed is :S , a , d , b , c , e.
Final Shortest Path tree
Visualization
• https://www.cs.usfca.edu/~galles/visualizatio
n/Dijkstra.html
• https://www.gatevidyalay.com/dijkstras-
algorithm-shortest-path-algorithm/

More Related Content

Similar to ppt 1.pptx

Graphs and eularian circuit & path with c++ program
Graphs and eularian circuit & path with c++ programGraphs and eularian circuit & path with c++ program
Graphs and eularian circuit & path with c++ programMuhammad Danish Badar
 
Graphs in datastructures
Graphs in datastructuresGraphs in datastructures
Graphs in datastructuresLikhithaGunturi
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structurePooja Bhojwani
 
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)Abdul Naqashbandi
 
Data structure computer graphs
Data structure computer graphsData structure computer graphs
Data structure computer graphsKumar
 
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjteUnit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjtepournima055
 
Lecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptxLecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptxking779879
 
Algorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptxAlgorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptxzerihunnana
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data StructureKeno benti
 
Graph Algorithms
Graph AlgorithmsGraph Algorithms
Graph AlgorithmsAshwin Shiv
 
graph representation.pdf
graph representation.pdfgraph representation.pdf
graph representation.pdfamitbhachne
 
Graph Basic In Data structure
Graph Basic In Data structureGraph Basic In Data structure
Graph Basic In Data structureIkhlas Rahman
 

Similar to ppt 1.pptx (20)

Graphs and eularian circuit & path with c++ program
Graphs and eularian circuit & path with c++ programGraphs and eularian circuit & path with c++ program
Graphs and eularian circuit & path with c++ program
 
Graphs
GraphsGraphs
Graphs
 
Graphs in datastructures
Graphs in datastructuresGraphs in datastructures
Graphs in datastructures
 
Graphs
GraphsGraphs
Graphs
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
 
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
 
Data structure computer graphs
Data structure computer graphsData structure computer graphs
Data structure computer graphs
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjteUnit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
 
Daa chpater 12
Daa chpater 12Daa chpater 12
Daa chpater 12
 
Lecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptxLecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptx
 
Algorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptxAlgorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptx
 
Graphs
GraphsGraphs
Graphs
 
UNIT 4_DSA_KAVITHA_RMP.ppt
UNIT 4_DSA_KAVITHA_RMP.pptUNIT 4_DSA_KAVITHA_RMP.ppt
UNIT 4_DSA_KAVITHA_RMP.ppt
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 
Graph Algorithms
Graph AlgorithmsGraph Algorithms
Graph Algorithms
 
graph representation.pdf
graph representation.pdfgraph representation.pdf
graph representation.pdf
 
Graph Basic In Data structure
Graph Basic In Data structureGraph Basic In Data structure
Graph Basic In Data structure
 

Recently uploaded

Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage examplePragyanshuParadkar1
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture designssuser87fa0c1
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 

Recently uploaded (20)

Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage example
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture design
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 

ppt 1.pptx

  • 2. • Graph is a non-linear data structure. It contains a set of points known as nodes (or vertices) and a set of links known as edges (or Arcs). Here edges are used to connect the vertices. • Graph is a collection of vertices and arcs in which vertices are connected with arcs
  • 3. Contd… • Graph is a collection of nodes and edges in which nodes are connected with edges • Generally, a graph G is represented as G = ( V , E ), where V is set of vertices and E is set of edges.
  • 4. Example • The following is a graph with 5 vertices and 6 edges. This graph G can be defined as G = ( V , E ) Where V = {A,B,C,D,E} and E = {(A,B),(A,C)(A,D),(B,D),(C,D),(B,E),(E,D)}.
  • 6. Graph Terminology Vertex Individual data element of a graph is called as Vertex. Vertex is also known as node. In above example graph, A, B, C, D & E are known as vertices.
  • 7. Edge • An edge is a connecting link between two vertices. Edge is also known as Arc. An edge is represented as (startingVertex, endingVertex). For example, in above graph the link between vertices A and B is represented as (A,B). In above example graph, there are 7 edges (i.e., (A,B), (A,C), (A,D), (B,D), (B,E), (C,D), (D,E)). Edges are three types. • Undirected Edge - An undirected egde is a bidirectional edge. If there is undirected edge between vertices A and B then edge (A , B) is equal to edge (B , A). • Directed Edge - A directed egde is a unidirectional edge. If there is directed edge between vertices A and B then edge (A , B) is not equal to edge (B , A). • Weighted Edge - A weighted egde is a edge with value (cost) on it.
  • 8. • Undirected Graph A graph with only undirected edges is said to be undirected graph. • Directed Graph A graph with only directed edges is said to be directed graph. • Mixed Graph A graph with both undirected and directed edges is said to be mixed graph.
  • 9. End vertices or Endpoints • The two vertices joined by edge are called end vertices (or endpoints) of that edge Origin • If a edge is directed, its first endpoint is said to be the origin of it. Destination • If a edge is directed, its first endpoint is said to be the origin of it and the other endpoint is said to be the destination of that edge.
  • 10. Adjacent • If there is an edge between vertices A and B then both A and B are said to be adjacent. In other words, vertices A and B are said to be adjacent if there is an edge between them. Incident • Edge is said to be incident on a vertex if the vertex is one of the endpoints of that edge. Outgoing Edge • A directed edge is said to be outgoing edge on its origin vertex.
  • 11. Incoming Edge • A directed edge is said to be incoming edge on its destination vertex. Degree • Total number of edges connected to a vertex is said to be degree of that vertex. Indegree • Total number of incoming edges connected to a vertex is said to be indegree of that vertex.
  • 12. Outdegree • Total number of outgoing edges connected to a vertex is said to be outdegree of that vertex. Parallel edges or Multiple edges • If there are two undirected edges with same end vertices and two directed edges with same origin and destination, such edges are called parallel edges or multiple edges. Self-loop • Edge (undirected or directed) is a self-loop if its two endpoints coincide with each other.
  • 13. Simple Graph • A graph is said to be simple if there are no parallel and self-loop edges. Path • A path is a sequence of alternate vertices and edges that starts at a vertex and ends at other vertex such that each edge is incident to its predecessor and successor vertex.
  • 14. Graph Representations Graph data structure is represented using following representations... • Adjacency Matrix • Incidence Matrix • Adjacency List
  • 15. Adjacency Matrix • In this representation, the graph is represented using a matrix of size total number of vertices by a total number of vertices. That means a graph with 4 vertices is represented using a matrix of size 4X4. In this matrix, both rows and columns represent vertices. This matrix is filled with either 1 or 0. Here, 1 represents that there is an edge from row vertex to column vertex and 0 represents that there is no edge from row vertex to column vertex. For example, consider the following undirected graph representation...
  • 18. Incidence Matrix • In this representation, the graph is represented using a matrix of size total number of vertices by a total number of edges. That means graph with 4 vertices and 6 edges is represented using a matrix of size 4X6. In this matrix, rows represent vertices and columns represents edges. This matrix is filled with 0 or 1 or -1. Here, 0 represents that the row edge is not connected to column vertex, 1 represents that the row edge is connected as the outgoing edge to column vertex and -1 represents that the row edge is connected as the incoming edge to column vertex. For example, consider the following directed graph representation...
  • 20. Adjacency List • In this representation, every vertex of a graph contains list of its adjacent vertices. For example, consider the following directed graph representation implemented using linked list...
  • 22. This representation can also be implemented using an array as follows..
  • 23. Graph Traversal • Graph traversal is a technique used for a searching vertex in a graph. The graph traversal is also used to decide the order of vertices is visited in the search process. A graph traversal finds the edges to be used in the search process without creating loops. That means using graph traversal we visit all the vertices of the graph without getting into looping path. There are two graph traversal techniques and they are as follows... • DFS (Depth First Search) • BFS (Breadth First Search)
  • 24. DFS (Depth First Search) • DFS traversal of a graph produces a spanning tree as final result. Spanning Tree is a graph without loops. We use Stack data structure with maximum size of total number of vertices in the graph to implement DFS traversal. We use the following steps to implement DFS traversal...
  • 25. Algorithm • Step 1 - Define a Stack of size total number of vertices in the graph. • Step 2 - Select any vertex as starting point for traversal. Visit that vertex and push it on to the Stack. • Step 3 - Visit any one of the non-visited adjacent vertices of a vertex which is at the top of stack and push it on to the stack. • Step 4 - Repeat step 3 until there is no new vertex to be visited from the vertex which is at the top of the stack. • Step 5 - When there is no new vertex to visit then use back tracking and pop one vertex from the stack. • Step 6 - Repeat steps 3, 4 and 5 until stack becomes Empty. • Step 7 - When stack becomes Empty, then produce final spanning tree by removing unused edges from the graph
  • 26. Back tracking is coming back to the vertex from which we reached the current vertex.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35. BFS (Breadth First Search) • BFS traversal of a graph produces a spanning tree as final result. Spanning Tree is a graph without loops. We use Queue data structure with maximum size of total number of vertices in the graph to implement BFS traversal.
  • 36. Algorithm Step 1 - Define a Queue of size total number of vertices in the graph. Step 2 - Select any vertex as starting point for traversal. Visit that vertex and insert it into the Queue. Step 3 - Visit all the non-visited adjacent vertices of the vertex which is at front of the Queue and insert them into the Queue. Step 4 - When there is no new vertex to be visited from the vertex which is at front of the Queue then delete that vertex. Step 5 - Repeat steps 3 and 4 until queue becomes empty. Step 6 - When queue becomes empty, then produce final spanning tree by removing unused edges from the graph
  • 38.
  • 39.
  • 40.
  • 41.
  • 43. TOPOLOGICAL SORTING • Topological Sorting is possible if and only if the graph is a Directed Acyclic Graph. • There may exist multiple different topological orderings for a given directed acyclic graph.
  • 44. Applications of Topological Sort- • Scheduling jobs from the given dependencies among jobs • Instruction Scheduling • Determining the order of compilation tasks to perform in makefiles • Data Serialization
  • 46. STEPS • Write in-degree of each vertex-
  • 47. STEP 2 • Vertex-A has the least in-degree. • So, remove vertex-A and its associated edges. • Now, update the in-degree of other vertices.
  • 49. STEP 3 • Vertex-A has the least in-degree. • So, remove vertex-A and its associated edges. • Now, update the in-degree of other vertices.
  • 50. STEP 4 • There are two vertices with the least in- degree. So, following 2 cases are possible- • In case-01, • Remove vertex-C and its associated edges. • Then, update the in-degree of other vertices. • In case-02, • Remove vertex-D and its associated edges. • Then, update the in-degree of other vertices.
  • 52. STEP 5 • Now, the above two cases are continued separately in the similar manner. • In case-01, • Remove vertex-D since it has the least in-degree. • Then, remove the remaining vertex-E. • In case-02, • Remove vertex-C since it has the least in-degree. • Then, remove the remaining vertex-E.
  • 54. CONCLUSION • For the given graph, following 2 different topological orderings are possible- • A B C D E • A B D C E https://www.cs.usfca.edu/~galles/visualization/ TopoSortIndegree.html https://www.gatevidyalay.com/topological-sort- topological-sorting/
  • 55. Dijsktra Algorithm • Dijkstra Algorithm is a very famous greedy algorithm. • It is used for solving the single source shortest path problem. • It computes the shortest path from one particular source node to all other remaining nodes of the graph.
  • 56. Conditions • It is important to note the following points regarding Dijkstra Algorithm- • Dijkstra algorithm works only for connected graphs. • Dijkstra algorithm works only for those graphs that do not contain any negative weight edge. • The actual Dijkstra algorithm does not output the shortest paths. • It only provides the value or cost of the shortest paths. • By making minor modifications in the actual algorithm, the shortest paths can be easily obtained. • Dijkstra algorithm works for directed as well as undirected graphs.
  • 57. Step-01 • In the first step. two sets are defined- • One set contains all those vertices which have been included in the shortest path tree. • In the beginning, this set is empty. • Other set contains all those vertices which are still left to be included in the shortest path tree. • In the beginning, this set contains all the vertices of the given graph.
  • 58. Step-02: • For each vertex of the given graph, two variables are defined as- • Π[v] which denotes the predecessor of vertex ‘v’ • d[v] which denotes the shortest path estimate of vertex ‘v’ from the source vertex. • Initially, the value of these variables is set as- • The value of variable ‘Π’ for each vertex is set to NIL i.e. Π[v] = NIL • The value of variable ‘d’ for source vertex is set to 0 i.e. d[S] = 0 • The value of variable ‘d’ for remaining vertices is set to ∞ i.e. d[v] = ∞
  • 59. Step-03: • The following procedure is repeated until all the vertices of the graph are processed- • Among unprocessed vertices, a vertex with minimum value of variable ‘d’ is chosen. • Its outgoing edges are relaxed. • After relaxing the edges for that vertex, the sets created in step-01 are updated.
  • 60. What is Edge Relaxation? • Consider the edge (a,b) in the following graph- • Here, d[a] and d[b] denotes the shortest path estimate for vertices a and b respectively from the source vertex ‘S’. • Now, • If d[a] + w < d[b] • then d[b] = d[a] + w and Π[b] = a • This is called as edge relaxation.
  • 62. STEP 1 • The following two sets are created- • Unvisited set : {S , a , b , c , d , e} • Visited set : { }
  • 63. STEP 2 • The two variables Π and d are created for each vertex and initialized as- • Π[S] = Π[a] = Π[b] = Π[c] = Π[d] = Π[e] = NIL • d[S] = 0 • d[a] = d[b] = d[c] = d[d] = d[e] = ∞
  • 64. STEP 3 • Vertex ‘S’ is chosen. • This is because shortest path estimate for vertex ‘S’ is least. • The outgoing edges of vertex ‘S’ are relaxed.
  • 65. Before Edge Relaxation • Now, • d[S] + 1 = 0 + 1 = 1 < ∞ • ∴ d[a] = 1 and Π[a] = S • d[S] + 5 = 0 + 5 = 5 < ∞ • ∴ d[b] = 5 and Π[b] = S
  • 66. After Edge Relaxation • After edge relaxation, our shortest path tree is- • Now, the sets are updated as- • Unvisited set : {a , b , c , d , e} • Visited set : {S}
  • 67. Step 04 • Vertex ‘a’ is chosen. • This is because shortest path estimate for vertex ‘a’ is least. • The outgoing edges of vertex ‘a’ are relaxed.
  • 68. Before Edge Relaxation • Now, • d[a] + 2 = 1 + 2 = 3 < ∞ • ∴ d[c] = 3 and Π[c] = a • d[a] + 1 = 1 + 1 = 2 < ∞ • ∴ d[d] = 2 and Π[d] = a • d[b] + 2 = 1 + 2 = 3 < 5 • ∴ d[b] = 3 and Π[b] = a
  • 70. After edge relaxation, our shortest path tree is-
  • 71. After edge relaxation • Now, the sets are updated as- • Unvisited set : {b , c , d , e} • Visited set : {S , a}
  • 72. Step-05 • Vertex ‘d’ is chosen. • This is because shortest path estimate for vertex ‘d’ is least. • The outgoing edges of vertex ‘d’ are relaxed.
  • 73. Before edge relaxation • Now, • d[d] + 2 = 2 + 2 = 4 < ∞ • ∴ d[e] = 4 and Π[e] = d • Now, the sets are updated as- • Unvisited set : {b , c , e} • Visited set : {S , a , d}
  • 75. Step 6 • Vertex ‘b’ is chosen. • This is because shortest path estimate for vertex ‘b’ is least. • Vertex ‘c’ may also be chosen since for both the vertices, shortest path estimate is least. • The outgoing edges of vertex ‘b’ are relaxed.
  • 76. Before Edge Relaxation • Now, • d[b] + 2 = 3 + 2 = 5 > 2 • ∴ No change • After edge relaxation, our shortest path tree remains the same as in Step-05. • Now, the sets are updated as- • Unvisited set : {c , e} • Visited set : {S , a , d , b}
  • 77. Step-07 • Vertex ‘c’ is chosen. • This is because shortest path estimate for vertex ‘c’ is least. • The outgoing edges of vertex ‘c’ are relaxed.
  • 78. Before Edge Relaxation • Now, • d[c] + 1 = 3 + 1 = 4 = 4 • ∴ No change • After edge relaxation, our shortest path tree remains the same as in Step-05. • Now, the sets are updated as- • Unvisited set : {e} • Visited set : {S , a , d , b , c}
  • 79. Step 8 • Vertex ‘e’ is chosen. • This is because shortest path estimate for vertex ‘e’ is least. • The outgoing edges of vertex ‘e’ are relaxed. • There are no outgoing edges for vertex ‘e’. • So, our shortest path tree remains the same as in Step-05. • Now, the sets are updated as- • Unvisited set : { } • Visited set : {S , a , d , b , c , e}
  • 80. Conclusion • Now, • All vertices of the graph are processed. • Our final shortest path tree is as shown below. • It represents the shortest path from source vertex ‘S’ to all other remaining vertices. • The order in which all the vertices are processed is :S , a , d , b , c , e.