Dijkstra's algorithm finds the shortest paths between vertices in a graph with non-negative edge weights. It works by maintaining distances from the source vertex to all other vertices, initially setting all distances to infinity except the source which is 0. It then iteratively selects the unvisited vertex with the lowest distance, marks it as visited, and updates the distances to its neighbors if a shorter path is found through the selected vertex. This continues until all vertices are visited, at which point the distances will be the shortest paths from the source vertex.
Bellman-Ford is an algorithm for finding shortest paths in a graph with positive or negative edge weights. It uses dynamic programming to iteratively update the shortest path costs from a source node to all other nodes. If the shortest path costs have not converged after |V|-1 iterations, there must be a negative weight cycle present in the graph. The Bellman-Ford algorithm returns both the shortest path costs and a predecessor graph that implicitly represents the shortest paths.
Graphs: Finding shortest paths
Topics:
Definitions
Floyd-Warshall algorithm
Dijkstra algorithm
Bellman-Ford algorithm
Teaching material for the course of "Tecniche di Programmazione" at Politecnico di Torino in year 2012/2013. More information: http://bit.ly/tecn-progr
The document describes algorithms for finding minimum spanning trees in graphs, including Prim's algorithm, Kruskal's algorithm, and a greedy algorithm. Prim's algorithm builds up the minimum spanning tree by repeatedly adding the lowest weight edge connected to the current tree. Kruskal's algorithm considers edges in order of weight and adds them if they do not create cycles. The greedy algorithm uses red and blue rules to color edges and proves that the blue edges will form a minimum spanning tree.
This document summarizes key concepts from a faculty development program on data structures, including graph applications, minimum spanning trees, shortest path algorithms, biconnected graphs, and Euler circuits. It provides examples and pseudocode for Prim's and Kruskal's minimum spanning tree algorithms and Dijkstra's shortest path algorithm. It also discusses identifying articulation points in a graph to determine if it is biconnected and conditions for the existence of Euler paths and circuits.
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...Khoa Mac Tu
This document compares the Bellman-Ford algorithm and Dijkstra's algorithm for finding shortest paths in graphs. Both algorithms can be used to find single-source shortest paths, but Bellman-Ford can handle graphs with negative edge weights while Dijkstra's algorithm cannot. Bellman-Ford has a worst-case time complexity of O(n^2) while Dijkstra's algorithm has a better worst-case time complexity of O(n^2). However, Dijkstra's algorithm is more efficient in practice for graphs with non-negative edge weights. The document provides pseudocode to describe the procedures of each algorithm.
This document describes Floyd's algorithm for solving the all-pairs shortest path problem in graphs. It begins with an introduction and problem statement. It then describes Dijkstra's algorithm as a greedy method for finding single-source shortest paths. It discusses graph representations and traversal methods. Finally, it provides pseudocode and analysis for Floyd's dynamic programming algorithm, which finds shortest paths between all pairs of vertices in O(n3) time.
GRAPH THEORY - Basic definition with examplesGayathri M
In this presentation, I have explained all the fundamentals of graph theory, from its definition to its types, and various ways to represent graphs as well.
a graph search algorithm that solves the single-source shortest path problem for a graph with non-negative edge path costs, producing a shortest path tree. This algorithm is often used in routing and as a subroutine in other graph algorithms.
This document discusses various greedy algorithms, including Dijkstra's algorithm for finding shortest paths in graphs. It provides details on Dijkstra's algorithm, including its greedy approach, proof of correctness, and efficient implementations using priority queues. It also discusses applications of shortest path algorithms and how the techniques extend to related problems involving closed semirings rather than just numbers.
The document discusses optimization problems and various graph search algorithms. It covers:
- Formulating optimization problems as finding the best solution from all feasible solutions.
- Graph search algorithms like breadth-first search and depth-first search that can be used to find optimal solutions by traversing graphs.
- Dijkstra's algorithm, a graph search algorithm that finds the shortest paths between nodes in a graph.
The document discusses shortest path algorithms for weighted graphs. It introduces Dijkstra's algorithm and the Bellman-Ford algorithm for finding shortest paths. Dijkstra's algorithm works for graphs with non-negative edge weights, while Bellman-Ford can handle graphs with negative edge weights. The document also describes how to find shortest paths in directed acyclic graphs and compute all-pairs shortest paths.
The document discusses algorithms for solving single-source shortest path problems on directed graphs. It begins by defining the problem and describing Bellman-Ford's algorithm, which can handle graphs with negative edge weights but runs in O(VE) time. It then describes how Dijkstra's algorithm improves this to O(ElogV) time using a priority queue, but requires non-negative edge weights. It also discusses how shortest paths can be found in O(V+E) time on directed acyclic graphs by relaxing edges topologically. Finally, it discusses how difference constraints can be modeled as shortest path problems on a corresponding constraint graph.
This document discusses several graph algorithms:
1) Topological sort is an ordering of the vertices of a directed acyclic graph (DAG) such that for every edge from vertex u to v, u comes before v in the ordering. It can be used to find a valid schedule respecting dependencies.
2) Strongly connected components are maximal subsets of vertices in a directed graph such that there is a path between every pair of vertices. An algorithm uses depth-first search to find SCCs in linear time.
3) Minimum spanning trees find a subset of edges that connects all vertices at minimum total cost. Prim's and Kruskal's algorithms find minimum spanning trees using greedy strategies in O(E
This document discusses several graph algorithms:
1) Topological sort is an ordering of the vertices of a directed acyclic graph (DAG) such that for every edge from vertex u to v, u comes before v in the ordering. It can be used to find a valid schedule respecting dependencies.
2) Strongly connected components are maximal subsets of vertices in a directed graph such that there is a path between every pair of vertices. An algorithm uses depth-first search to find SCCs in linear time.
3) Minimum spanning trees find a subset of edges that connects all vertices at minimum total cost. Prim's and Kruskal's algorithms find minimum spanning trees using greedy strategies in O(E
The document discusses shortest path algorithms for graphs. It defines different variants of shortest path problems like single-source, single-destination, and all-pairs shortest paths. It presents algorithms like Bellman-Ford, Dijkstra's algorithm, and Floyd-Warshal algorithm to solve these problems. Bellman-Ford handles negative edge weights but has a higher time complexity of O(V^3) compared to Dijkstra's which only works for positive edges. Floyd-Warshal solves the all-pairs shortest paths problem in O(V^3) time using dynamic programming and matrix multiplication.
The document discusses randomized graph algorithms and techniques for analyzing them. It describes a linear time algorithm for finding minimum spanning trees (MST) that samples edges and uses Boruvka's algorithm and edge filtering. It also discusses Karger's algorithm for approximating the global minimum cut in near-linear time using edge contractions. Finally, it presents an approach for 3-approximate distance oracles that preprocesses a graph to build a data structure for answering approximate shortest path queries in constant time using landmark vertices and storing local and global distance information.
This document discusses dynamic programming and algorithms for solving all-pair shortest path problems. It begins by defining dynamic programming as avoiding recalculating solutions by storing results in a table. It then describes Floyd's algorithm for finding shortest paths between all pairs of nodes in a graph. The algorithm iterates through nodes, calculating shortest paths that pass through each intermediate node. It takes O(n3) time for a graph with n nodes. Finally, it discusses the multistage graph problem and provides forward and backward algorithms to find the minimum cost path from source to destination in a multistage graph in O(V+E) time, where V and E are the numbers of vertices and edges.
This document provides an overview of graphs and graph algorithms. It begins with an introduction to graphs, including definitions of vertices, edges, directed/undirected graphs, and graph representations using adjacency matrices and lists. It then covers graph traversal algorithms like depth-first search and breadth-first search. Minimum spanning trees and algorithms for finding them like Kruskal's algorithm are also discussed. The document provides examples and pseudocode for the algorithms. It analyzes the time complexity of the graph algorithms. Overall, the document provides a comprehensive introduction to fundamental graph concepts and algorithms.
This document discusses graphs and graph theory concepts. It defines terms like paths, circuits, connectivity, Euler paths and circuits, Hamilton paths and circuits. It also describes Dijkstra's algorithm for finding the shortest path between two vertices in a weighted graph. Several examples are provided to illustrate graph concepts and applying Dijkstra's algorithm to find shortest paths in weighted graphs.
In mathematics, graph theory is the study of graphs, which are mathematical structures used to model pairwise relations between objects.Graph theory is also important in real life.
This document discusses algorithms for solving the all pairs shortest path problem. It introduces the all pair distance problem for unweighted graphs, which can be solved by multiplying the adjacency matrix. For weighted graphs, it describes the randomized Boolean product witness matrix algorithm, which reduces the all pairs shortest path problem to matrix multiplication. Deterministic algorithms like Floyd-Warshall are also discussed.
Form View Attributes in Odoo 18 - Odoo SlidesCeline George
Odoo is a versatile and powerful open-source business management software, allows users to customize their interfaces for an enhanced user experience. A key element of this customization is the utilization of Form View attributes.
This document summarizes key concepts from a faculty development program on data structures, including graph applications, minimum spanning trees, shortest path algorithms, biconnected graphs, and Euler circuits. It provides examples and pseudocode for Prim's and Kruskal's minimum spanning tree algorithms and Dijkstra's shortest path algorithm. It also discusses identifying articulation points in a graph to determine if it is biconnected and conditions for the existence of Euler paths and circuits.
A study on_contrast_and_comparison_between_bellman-ford_algorithm_and_dijkstr...Khoa Mac Tu
This document compares the Bellman-Ford algorithm and Dijkstra's algorithm for finding shortest paths in graphs. Both algorithms can be used to find single-source shortest paths, but Bellman-Ford can handle graphs with negative edge weights while Dijkstra's algorithm cannot. Bellman-Ford has a worst-case time complexity of O(n^2) while Dijkstra's algorithm has a better worst-case time complexity of O(n^2). However, Dijkstra's algorithm is more efficient in practice for graphs with non-negative edge weights. The document provides pseudocode to describe the procedures of each algorithm.
This document describes Floyd's algorithm for solving the all-pairs shortest path problem in graphs. It begins with an introduction and problem statement. It then describes Dijkstra's algorithm as a greedy method for finding single-source shortest paths. It discusses graph representations and traversal methods. Finally, it provides pseudocode and analysis for Floyd's dynamic programming algorithm, which finds shortest paths between all pairs of vertices in O(n3) time.
GRAPH THEORY - Basic definition with examplesGayathri M
In this presentation, I have explained all the fundamentals of graph theory, from its definition to its types, and various ways to represent graphs as well.
a graph search algorithm that solves the single-source shortest path problem for a graph with non-negative edge path costs, producing a shortest path tree. This algorithm is often used in routing and as a subroutine in other graph algorithms.
This document discusses various greedy algorithms, including Dijkstra's algorithm for finding shortest paths in graphs. It provides details on Dijkstra's algorithm, including its greedy approach, proof of correctness, and efficient implementations using priority queues. It also discusses applications of shortest path algorithms and how the techniques extend to related problems involving closed semirings rather than just numbers.
The document discusses optimization problems and various graph search algorithms. It covers:
- Formulating optimization problems as finding the best solution from all feasible solutions.
- Graph search algorithms like breadth-first search and depth-first search that can be used to find optimal solutions by traversing graphs.
- Dijkstra's algorithm, a graph search algorithm that finds the shortest paths between nodes in a graph.
The document discusses shortest path algorithms for weighted graphs. It introduces Dijkstra's algorithm and the Bellman-Ford algorithm for finding shortest paths. Dijkstra's algorithm works for graphs with non-negative edge weights, while Bellman-Ford can handle graphs with negative edge weights. The document also describes how to find shortest paths in directed acyclic graphs and compute all-pairs shortest paths.
The document discusses algorithms for solving single-source shortest path problems on directed graphs. It begins by defining the problem and describing Bellman-Ford's algorithm, which can handle graphs with negative edge weights but runs in O(VE) time. It then describes how Dijkstra's algorithm improves this to O(ElogV) time using a priority queue, but requires non-negative edge weights. It also discusses how shortest paths can be found in O(V+E) time on directed acyclic graphs by relaxing edges topologically. Finally, it discusses how difference constraints can be modeled as shortest path problems on a corresponding constraint graph.
This document discusses several graph algorithms:
1) Topological sort is an ordering of the vertices of a directed acyclic graph (DAG) such that for every edge from vertex u to v, u comes before v in the ordering. It can be used to find a valid schedule respecting dependencies.
2) Strongly connected components are maximal subsets of vertices in a directed graph such that there is a path between every pair of vertices. An algorithm uses depth-first search to find SCCs in linear time.
3) Minimum spanning trees find a subset of edges that connects all vertices at minimum total cost. Prim's and Kruskal's algorithms find minimum spanning trees using greedy strategies in O(E
This document discusses several graph algorithms:
1) Topological sort is an ordering of the vertices of a directed acyclic graph (DAG) such that for every edge from vertex u to v, u comes before v in the ordering. It can be used to find a valid schedule respecting dependencies.
2) Strongly connected components are maximal subsets of vertices in a directed graph such that there is a path between every pair of vertices. An algorithm uses depth-first search to find SCCs in linear time.
3) Minimum spanning trees find a subset of edges that connects all vertices at minimum total cost. Prim's and Kruskal's algorithms find minimum spanning trees using greedy strategies in O(E
The document discusses shortest path algorithms for graphs. It defines different variants of shortest path problems like single-source, single-destination, and all-pairs shortest paths. It presents algorithms like Bellman-Ford, Dijkstra's algorithm, and Floyd-Warshal algorithm to solve these problems. Bellman-Ford handles negative edge weights but has a higher time complexity of O(V^3) compared to Dijkstra's which only works for positive edges. Floyd-Warshal solves the all-pairs shortest paths problem in O(V^3) time using dynamic programming and matrix multiplication.
The document discusses randomized graph algorithms and techniques for analyzing them. It describes a linear time algorithm for finding minimum spanning trees (MST) that samples edges and uses Boruvka's algorithm and edge filtering. It also discusses Karger's algorithm for approximating the global minimum cut in near-linear time using edge contractions. Finally, it presents an approach for 3-approximate distance oracles that preprocesses a graph to build a data structure for answering approximate shortest path queries in constant time using landmark vertices and storing local and global distance information.
This document discusses dynamic programming and algorithms for solving all-pair shortest path problems. It begins by defining dynamic programming as avoiding recalculating solutions by storing results in a table. It then describes Floyd's algorithm for finding shortest paths between all pairs of nodes in a graph. The algorithm iterates through nodes, calculating shortest paths that pass through each intermediate node. It takes O(n3) time for a graph with n nodes. Finally, it discusses the multistage graph problem and provides forward and backward algorithms to find the minimum cost path from source to destination in a multistage graph in O(V+E) time, where V and E are the numbers of vertices and edges.
This document provides an overview of graphs and graph algorithms. It begins with an introduction to graphs, including definitions of vertices, edges, directed/undirected graphs, and graph representations using adjacency matrices and lists. It then covers graph traversal algorithms like depth-first search and breadth-first search. Minimum spanning trees and algorithms for finding them like Kruskal's algorithm are also discussed. The document provides examples and pseudocode for the algorithms. It analyzes the time complexity of the graph algorithms. Overall, the document provides a comprehensive introduction to fundamental graph concepts and algorithms.
This document discusses graphs and graph theory concepts. It defines terms like paths, circuits, connectivity, Euler paths and circuits, Hamilton paths and circuits. It also describes Dijkstra's algorithm for finding the shortest path between two vertices in a weighted graph. Several examples are provided to illustrate graph concepts and applying Dijkstra's algorithm to find shortest paths in weighted graphs.
In mathematics, graph theory is the study of graphs, which are mathematical structures used to model pairwise relations between objects.Graph theory is also important in real life.
This document discusses algorithms for solving the all pairs shortest path problem. It introduces the all pair distance problem for unweighted graphs, which can be solved by multiplying the adjacency matrix. For weighted graphs, it describes the randomized Boolean product witness matrix algorithm, which reduces the all pairs shortest path problem to matrix multiplication. Deterministic algorithms like Floyd-Warshall are also discussed.
Form View Attributes in Odoo 18 - Odoo SlidesCeline George
Odoo is a versatile and powerful open-source business management software, allows users to customize their interfaces for an enhanced user experience. A key element of this customization is the utilization of Form View attributes.
Ajanta Paintings: Study as a Source of HistoryVirag Sontakke
This Presentation is prepared for Graduate Students. A presentation that provides basic information about the topic. Students should seek further information from the recommended books and articles. This presentation is only for students and purely for academic purposes. I took/copied the pictures/maps included in the presentation are from the internet. The presenter is thankful to them and herewith courtesy is given to all. This presentation is only for academic purposes.
The role of wall art in interior designingmeghaark2110
Wall patterns are designs or motifs applied directly to the wall using paint, wallpaper, or decals. These patterns can be geometric, floral, abstract, or textured, and they add depth, rhythm, and visual interest to a space.
Wall art and wall patterns are not merely decorative elements, but powerful tools in shaping the identity, mood, and functionality of interior spaces. They serve as visual expressions of personality, culture, and creativity, transforming blank and lifeless walls into vibrant storytelling surfaces. Wall art, whether abstract, realistic, or symbolic, adds emotional depth and aesthetic richness to a room, while wall patterns contribute to structure, rhythm, and continuity in design. Together, they enhance the visual experience, making spaces feel more complete, welcoming, and engaging. In modern interior design, the thoughtful integration of wall art and patterns plays a crucial role in creating environments that are not only beautiful but also meaningful and memorable. As lifestyles evolve, so too does the art of wall decor—encouraging innovation, sustainability, and personalized expression within our living and working spaces.
How to Share Accounts Between Companies in Odoo 18Celine George
In this slide we’ll discuss on how to share Accounts between companies in odoo 18. Sharing accounts between companies in Odoo is a feature that can be beneficial in certain scenarios, particularly when dealing with Consolidated Financial Reporting, Shared Services, Intercompany Transactions etc.
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18Celine George
In this slide, we’ll discuss on how to clean your contacts using the Deduplication Menu in Odoo 18. Maintaining a clean and organized contact database is essential for effective business operations.
*"Sensing the World: Insect Sensory Systems"*Arshad Shaikh
Insects' major sensory organs include compound eyes for vision, antennae for smell, taste, and touch, and ocelli for light detection, enabling navigation, food detection, and communication.
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Leonel Morgado
Slides used at the Invited Talk at the Harvard - Education University of Hong Kong - Stanford Joint Symposium, "Emerging Technologies and Future Talents", 2025-05-10, Hong Kong, China.
Search Matching Applicants in Odoo 18 - Odoo SlidesCeline George
The "Search Matching Applicants" feature in Odoo 18 is a powerful tool that helps recruiters find the most suitable candidates for job openings based on their qualifications and experience.
How to Configure Public Holidays & Mandatory Days in Odoo 18Celine George
In this slide, we’ll explore the steps to set up and manage Public Holidays and Mandatory Days in Odoo 18 effectively. Managing Public Holidays and Mandatory Days is essential for maintaining an organized and compliant work schedule in any organization.
What is the Philosophy of Statistics? (and how I was drawn to it)jemille6
What is the Philosophy of Statistics? (and how I was drawn to it)
Deborah G Mayo
At Dept of Philosophy, Virginia Tech
April 30, 2025
ABSTRACT: I give an introductory discussion of two key philosophical controversies in statistics in relation to today’s "replication crisis" in science: the role of probability, and the nature of evidence, in error-prone inference. I begin with a simple principle: We don’t have evidence for a claim C if little, if anything, has been done that would have found C false (or specifically flawed), even if it is. Along the way, I’ll sprinkle in some autobiographical reflections.
How to Create Kanban View in Odoo 18 - Odoo SlidesCeline George
The Kanban view in Odoo is a visual interface that organizes records into cards across columns, representing different stages of a process. It is used to manage tasks, workflows, or any categorized data, allowing users to easily track progress by moving cards between stages.
Struggling with your botany assignments? This comprehensive guide is designed to support college students in mastering key concepts of plant biology. Whether you're dealing with plant anatomy, physiology, ecology, or taxonomy, this guide offers helpful explanations, study tips, and insights into how assignment help services can make learning more effective and stress-free.
📌What's Inside:
• Introduction to Botany
• Core Topics covered
• Common Student Challenges
• Tips for Excelling in Botany Assignments
• Benefits of Tutoring and Academic Support
• Conclusion and Next Steps
Perfect for biology students looking for academic support, this guide is a useful resource for improving grades and building a strong understanding of botany.
WhatsApp:- +91-9878492406
Email:- [email protected]
Website:- https://onlinecollegehomeworkhelp.com/botany-homework-help
Rock Art As a Source of Ancient Indian HistoryVirag Sontakke
This Presentation is prepared for Graduate Students. A presentation that provides basic information about the topic. Students should seek further information from the recommended books and articles. This presentation is only for students and purely for academic purposes. I took/copied the pictures/maps included in the presentation are from the internet. The presenter is thankful to them and herewith courtesy is given to all. This presentation is only for academic purposes.
Classification of mental disorder in 5th semester bsc. nursing and also used ...parmarjuli1412
Classification of mental disorder in 5th semester Bsc. Nursing and also used in 2nd year GNM Nursing Included topic is ICD-11, DSM-5, INDIAN CLASSIFICATION, Geriatric-psychiatry, review of personality development, different types of theory, defense mechanism, etiology and bio-psycho-social factors, ethics and responsibility, responsibility of mental health nurse, practice standard for MHN, CONCEPTUAL MODEL and role of nurse, preventive psychiatric and rehabilitation, Psychiatric rehabilitation,
Classification of mental disorder in 5th semester bsc. nursing and also used ...parmarjuli1412
Ad
Lecture 10 - Graph part 2.pptx,discrete mathemactics
1. Ta Quang Hung
FACULTY OF INFORMATION TECHNOLOGY
Discrete Mathematics
Fall, 2023
Lecture 10: Graph – Part 2
2. Lecture 10 The University of New South Wales
Discrete Mathematics
Contents
Page 2
2 Dijkstras Algorithm
3 Bellman-Ford Algorithm
1 Shortest Path Algorithms
4 Summary
3. Lecture 10 The University of New South Wales
Discrete Mathematics Page 3
SHORTEST PATH
ALGORITHMS
4. Lecture 10 The University of New South Wales
Discrete Mathematics
Introduction
• Perhaps the most intuitive graph-processing
problem is one that you encounter regularly, when
using a map application or a navigation system to
get directions from one place to another. A graph
model is immediate: vertices correspond to
intersections and edges correspond to roads, with
weights on the edges that model the cost, perhaps
distance or travel time. The possibility of one-way
roads means that we will need to consider edge-
weighted digraphs. In this model, the problem is
easy to formulate:
Find the lowest-cost way to get
from one vertex to another
Page 4
5. Lecture 10 The University of New South Wales
Discrete Mathematics
Example: Navigation System
Page 5
Vertices correspond to
intersections and edges
correspond to roads.
Weights on the edges that
model the cost, perhaps
distance or travel time.
6. Lecture 10 The University of New South Wales
Discrete Mathematics
Example: Geographic Information System (GIS)
Page 6
Multiple layers are
combined to build GIS
7. Lecture 10 The University of New South Wales
Discrete Mathematics
Example: Printed Circuit Board (PCB)
Page 7
Autoroute Mode: Find the
shortest path for connections to
minimize resistances.
8. Lecture 10 The University of New South Wales
Discrete Mathematics
Example: PCB layout for DDR3 RAM interface
Page 8
9. Lecture 10 The University of New South Wales
Discrete Mathematics
Example: Game
Page 9
Base under attack: Troops must
find the shortest path back home.
Game map:
Look like a
maze.
10. Lecture 10 The University of New South Wales
Discrete Mathematics
Example: Uber Taxi
Page 10
Fare Estimate: calculate the minimum fee for the trip.
11. Lecture 10 The University of New South Wales
Discrete Mathematics
Properties of Shortest Path
• Paths are directed. A shortest path must respect the
direction of its edges.
• The weights are not necessarily distances. We use
examples where vertices are points in the plane and weights
are Euclidean distances, such as the digraph on the facing
page. But the weights might represent time or cost or an
entirely different variable and do not need to be proportional
to a distance at all. We are emphasizing this point by using
mixed-metaphor terminology where we refer to a shortest
path of minimal weight or cost.
• Not all vertices need be reachable. If t is not reachable
from s, there is no path at all, and therefore there is no
shortest path from s to t. For simplicity, our small running
example is strongly connected (every vertex is reachable
from every other vertex).
Page 11
12. Lecture 10 The University of New South Wales
Discrete Mathematics
Properties of Shortest Path (con't)
• Negative weights introduce complications. For the moment, we
assume that edge weights are positive (or zero).
• Shortest paths are normally simple. Algorithms ignore zero-weight
edges that form cycles, so that the shortest paths they find have no
cycles.
• Shortest paths are not necessarily unique. There may be multiple
paths of the lowest weight from one vertex to another; we are content to
find any one of them.
• Parallel edges and self-loops may be present. Only the lowest-weight
among a set of parallel edges will play a role, and no shortest path
contains a self-loop (except possibly one of zero weight, which we
ignore). In the text, we implicitly assume that parallel edges are not
present for convenience in using the notation v->w to refer
unambiguously to the edge from v to w.
Page 12
13. Lecture 10 The University of New South Wales
Discrete Mathematics Page 13
DIJKSTRAS ALGORITHM
14. Lecture 10 The University of New South Wales
Discrete Mathematics
Dijkstras Algorithm
Page 14
• This algorithm can’t work with negative edge
weights.
• Dijkstras algorithm runs in O(|n|+|m|log|m|) where n
vertices and m edges.
15. Lecture 10 The University of New South Wales
Discrete Mathematics
Dijkstras Algorithm: Example
Page 15
S = {A}
Find path from A to all other vertices?
Set L(A) = 0
Step 0
A
B
C D
E
G
F
3
6
2
7
1
2
4 5
6
8
9
3
3
Solved node
Unsolved node
Solving node
Denote:
• w(A,B) = weighting coefficient in the
path from A to B. So, from the graph,
we have w(A,B) = 3.
• L(Y) = Label of Y. Minimum cost of Y
or the length of the shortest path
from orignating vertice to Y. L(Y) is a
sum of all weighting coefficient and
the cost of all vertices on the shortest
path.
• S: A set of all vertices that the
shortest path go through.
16. Lecture 10 The University of New South Wales
Discrete Mathematics
Dijkstras Algorithm: Example
Page 16
S = {A,G}
Check all paths from A to
adjiacent vertices: F, G, B, C
L(A) + w(A,G) = 1
L(A) + w(A,B) = 3
L(A) + w(A,F) = 2
L(A) + w(A,C) = 7
Next vertice is G, add G to set S
Set L(G) = 1
A
B
C D
E
G
F
Solved node
Unsolved node
3
6
2
7
1
2
4 5
6
8
9
3
3
Solving node
0
Step 1
17. Lecture 10 The University of New South Wales
Discrete Mathematics
Dijkstras Algorithm: Example
Page 17
S = {A,G,F}
Check all paths from A, G: F, B, E, D,
C.
L(G) + w(G,F) = 5
L(G) + w(G,E) = 10
L(G) + w(G,D) = 9
L(G) + w(G,B) = 3
Next vertice is F, set L(F)=2, add F
to set S:
A
B
C D
E
G
F
Solved node
Unsolved node
3
6
2
7
1
2
4 5
6
8
9
3
3
Solving node
0
1
L(A) + w(A,B) = 3
L(A) + w(A,F) = 2
L(A) + w(A,C) = 7
Step 2
18. Lecture 10 The University of New South Wales
Discrete Mathematics
Dijkstras Algorithm: Example
Page 18
A
B
C D
E
G
F
Solved node
Unsolved node
3
6
2
7
1
2
4 5
6
8
9
3
3
Solving node
0
1
2
S = {A,G,F,B}
Check all paths from A, G, F to
adjiacent vertices: B, E, C, D.
L(G) + w(G,E) = 10
L(G) + w(G,D) = 9
L(G) + w(G,B) = 3
Next vertice is B, set L(B) = 3, add B
to set S:
L(A) + w(A,B) = 3
L(A) + w(A,C) = 7
L(F) + w(F,E) = 7
L(F) + w(F,D) = 5
Step 3
19. Lecture 10 The University of New South Wales
Discrete Mathematics
Dijkstras Algorithm: Example
Page 19
S = {A,G,F,B,D}
Check all paths from A, G, F, B to
adjiacent vertices: E, C, D.
L(G) + w(G,E) = 10
L(G) + w(G,D) = 9
Next vertice is D, set L(D) = 5, add D
to set S:
L(A) + w(A,C) = 7
L(F) + w(F,E) = 7
L(F) + w(F,D) = 5
L(B) + w(B,C) = 9
A
B
C D
E
G
F
Solved node
Unsolved node
3
6
2
7
1
2
4 5
6
8
9
3
3
Solving node
0
1
2
3
Step 4
20. Lecture 10 The University of New South Wales
Discrete Mathematics
Dijkstras Algorithm: Example
Page 20
S = {A,G,F,B,D,C}
Check all paths from A, G, F, B, D to
adjiacent vertices: E, C.
L(G) + w(G,E) = 10
Next vertice is C, set L(C) = 7, add C
to set S:
L(A) + w(A,C) = 7
L(F) + w(F,E) = 7
L(B) + w(B,C) = 9
A
B
C D
E
G
F
Solved node
Unsolved node
3
6
2
7
1
2
4 5
6
8
9
3
3
Solving node
0
1
2
3
5
L(D) + w(D,C) = 8
L(D) + w(D,E) = 11
Step 5
21. Lecture 10 The University of New South Wales
Discrete Mathematics
Dijkstras Algorithm: Example
Page 21
S = {A,G,F,B,D,C,E}
Check all paths from A, G, F, B, D, C
to adjiacent vertices: E.
L(G) + w(G,E) = 10
Next vertice is E, set L(E) = 7, add E
to set S:
L(F) + w(F,E) = 7
L(D) + w(D,E) = 11
A
B
C D
E
G
F
Solved node
Unsolved node
3
6
2
7
1
2
4 5
6
8
9
3
3
Solving node
0
1
2
3
5
7
Step 6
22. Lecture 10 The University of New South Wales
Discrete Mathematics
Dijkstras Algorithm: Example
Page 22
n A G F B D C E Shortest Path
0 0 A
1 0 (1,A) (2,A) (3,A) (7,A) A,G
2 0 (1,A) (2,A) (3,A) (9,G) (7,A) (10,G) A,G,F
3 0 (1,A) (2,A) (3,A) (5,F) (7,A) (7,F) A,G,F,B
4 0 (1,A) (2,A) (3,A) (5,F) (7,A) (7,F) A,G,F,B,D
5 0 (1,A) (2,A) (3,A) (5,F) (7,A) (7,F) A,G,F,B,D,C
6 0 (1,A) (2,A) (3,A) (5,F) (7,A) (7,F) A,G,F,B,D,C,E
0 (1,A) (2,A) (3,A) (5,F) (7,A) (7,F) A,G,F,B,D,C,E
Denote:
• (L,P): where L is label of the vertice, and P is preceding vertice in the path.
23. Lecture 10 The University of New South Wales
Discrete Mathematics Page 23
BELLMAN-FORD
ALGORITHM
24. Lecture 10 The University of New South Wales
Discrete Mathematics
Bellman-Ford Algorithm
Page 24
• This algorithm can work with negative edge weights.
• Can detect a negative cycle.
• Bellman-Ford runs in O(|n|-|m|) where n vertices and
m edges.
25. Lecture 10 The University of New South Wales
Discrete Mathematics
Bellman-Ford Algorithm: Example
Page 25
Find path from Z to all other vertices?
U
Z
X Y
V
6
7
8
2
5
-3
-4
7
9
Solved node
Unsolved node
Solving node
Denote:
• w(A,B) = weighting coefficient in the
path from A to B. So, from the graph,
we have w(A,B) = 3.
-2
26. Lecture 10 The University of New South Wales
Discrete Mathematics
Bellman-Ford Algorithm: Example
Page 26
Step 0
U
Z
X Y
V
6
7
8
2
5
-3
-4
7
9
Solved node
Unsolved node
Solving node
-2
n Z U X V Y
0 0 ,- ,- ,- ,-
0
27. Lecture 10 The University of New South Wales
Discrete Mathematics
Bellman-Ford Algorithm: Example
Page 27
Step 1
U
Z
X Y
V
6
7
8
2
5
-3
-4
7
9
Solved node
Unsolved node
Solving node
-2
6,Z
7,Z
n Z U X V Y
0 0 ,- ,- ,- ,-
1 0 6,Z 7,Z ,- ,-
From Z, can go to U, and X. Then assign
labels to U, X by adding cost of Z
(in previous step – step 0) to w(Z,U) and
w(Z,X). We denote
0
L1(U) = 6; P1(U) = Z
L1(X) = 7; P1(X) = Z
28. Lecture 10 The University of New South Wales
Discrete Mathematics
Bellman-Ford Algorithm: Example
Page 28
Step 2
U
Z
X Y
V
6
7
8
2
5
-3
-4
7
9
Solved node
Unsolved node
Solving node
-2
6,Z 4,X
2,U
7,Z
n Z U X V Y
0 0 ,- ,- ,- ,-
1 0 6,Z 7,Z ,- ,-
2 0 6,Z 7,Z 4,X 2,U
0
Recalculate U,X,V,Y by (L,P) in step 2:
L2(V) = min{L1(U)+w(U,V),L1(X)+w(X,V)}
= min{6+5,7-3} = 4, P2(V) = X
L2(Y) = min{L1(U)+w(U,Y), L1(X)+w(X,Y),
= min{6-4,7+9} = 2; P2(Y) = U
L2(U) = min{L1(Z)+w(Z,U),L1(V)+w(V,U)}
= min{0+6, } = 6; P2(U) = Z
L2(X) = min{L1(U)+w(U,X), L1(Z)+w(Z,X),
= min{6+8,0+7} = 7; P2(X) = Z
29. Lecture 10 The University of New South Wales
Discrete Mathematics
Bellman-Ford Algorithm: Example
Page 29
Step 3
U
Z
X Y
V
6
7
8
2
5
-3
-4
7
9
Solved node
Unsolved node
Solving node
-2
2,V 4,X
2,U
7,Z
n Z U X V Y
0 0 ,- ,- ,- ,-
1 0 6,Z 7,Z ,- ,-
2 0 6,Z 7,Z 4,X 2,U
3 0 2,V 7,Z 4,X 2,U
0
Recalculate U,X,V,Y by (L,P) in step 3:
L3(V) = min{L2(U)+w(U,V),L2(X)+w(X,V)}
= min{6+5,7-3} = 4, P3(V) = 4,X
L3(Y) = min{L2(U)+w(U,Y), L2(X)+w(X,Y),
= min{6-4,7+9} = 2; P3(Y) = 2,U
L3(U) = min{L2(Z)+w(Z,U),L2(V)+w(V,U)}
= min{0+6,4-2} = 2; P3(U) = 2,V
L3(X) = min{L2(U)+w(U,X), L2(Z)+w(Z,X),
= min{6+8,0+7} = 7; P3(X) = 7,Z
30. Lecture 10 The University of New South Wales
Discrete Mathematics
Bellman-Ford Algorithm: Example
Page 30
Step 4
U
Z
X Y
V
6
7
8
2
5
-3
-4
7
9
Solved node
Unsolved node
Solving node
-2
2,V 4,X
2,U
7,Z
n Z U X V Y
0 0 ,- ,- ,- ,-
1 0 6,Z 7,Z ,- ,-
2 0 6,Z 7,Z 4,X 2,U
3 0 2,V 7,Z 4,X 2,U
4 0 2,V 7,Z 4,X -2,U
0
Recalculate U,X,V,Y by (L,P) in step 3:
L4(V) = min{L3(U)+w(U,V),L3(X)+w(X,V)}
= min{2+5,7-3} = 4, P4(V) = X
L4(Y) = min{L3(U)+w(U,Y), L3(X)+w(X,Y),
= min{2-4,7+9} = -2; P4(Y) = U
L4(U) = min{L3(Z)+w(Z,U),L3(V)+w(V,U)}
= min{0+6,4-2} = 2; P4(U) = V
L4(X) = min{L3(U)+w(U,X), L3(Z)+w(Z,X),
= min{2+8,0+7} = 7; P4(X) = Z
31. Lecture 10 The University of New South Wales
Discrete Mathematics
Summary
Page 31
2 Dijkstras Algorithm
3 Bellman-Ford Algorithm
1 Shortest Path Algorithms