Skip to content

Commit 65406cd

Browse files
authored
Typo: breadth (first traversal), not breath
1 parent c964541 commit 65406cd

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

Chapters/Chapter5/chapter5.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Shortest path problemThe shortest path problem consists on finding a path between two pairs of nodes in which the sumof the weights is minimized. For a general graph this problem is NP-hard. For some kind of graphs this problemcan be solved in linear time.In this chapter we will present multiple algorithms:- A version implementing breath first traversal,- Dijkstra's algorithm for weighted nodes,- an algorithm for Directed Acyclic Graphs and,- Bellman-Ford algorithm for negative weighted graphs.### ExamplesLet us take this graph as an example *@short_distance_graph@* As it is an unweighted graph, we can calculate the distance between 2 nodes using the BFS algorithm.![Short distance graph.](figures/generic_example.pdf width=48&label=short_distance_graph)But, if we add weights to the graph, as in *@short_distance_graph_2@* we cannot no longer use BFS. But we can find the shortest distance using the Dijkstra algorithm.![Short distance with weights.](figures/dag.pdf width=48&label=short_distance_graph_2)The Dijkstra's algorithm does not work on graphs with negative weights. So, if we add negative weights to the graph, we must use the Bellman-Fordalgorithm to solve the problem. Figure *@short_distance_graph_3@*![Short distance with negative weights.](figures/longest_path.pdf width=48&label=short_distance_graph_3)If the graph has no cycles, a Directed Acyclic Graph \(DAG\) \(like Figure *@short_distance_graph_3@*\), we can use an algorithm based on topological sort to findthe shortest distance. This algorithm works for both negative and positive weights as long the graph has no cycles. This algorithm is betterin terms of time complexity but is more restricted as it only runs on DAG. On the other hand, Dijkstra's and Bellman-Ford both run in both cyclic and acyclic graphs.### Shortest path on unweighted graphs \(BFS algorithm\)If the graph is unweighted or all edges have the same **non-negative** weight, the shortest path can be foundin linear time $O(V + E)$ using Breadth First Search \(BFS\) algorithm.BFS is an algorithm for traveling a graph in a traversal way. That means that the algorithm will travel the children of the starting node always in orderensuring that when the goal node, if exists, is founded the path will be the shortest one possible.BFS is a single source shortest path algorithm. That means that before running the algorithm it is neededto specify a starting node. Then, the algorithm can tell us the shortest path between the starting nodeand all the other nodes.The algorithm is the following one:```initialize a queue Q
1+
## Shortest path problemThe shortest path problem consists on finding a path between two pairs of nodes in which the sumof the weights is minimized. For a general graph this problem is NP-hard. For some kind of graphs this problemcan be solved in linear time.In this chapter we will present multiple algorithms:- A version implementing breadth first traversal,- Dijkstra's algorithm for weighted nodes,- an algorithm for Directed Acyclic Graphs and,- Bellman-Ford algorithm for negative weighted graphs.### ExamplesLet us take this graph as an example *@short_distance_graph@* As it is an unweighted graph, we can calculate the distance between 2 nodes using the BFS algorithm.![Short distance graph.](figures/generic_example.pdf width=48&label=short_distance_graph)But, if we add weights to the graph, as in *@short_distance_graph_2@* we cannot no longer use BFS. But we can find the shortest distance using the Dijkstra algorithm.![Short distance with weights.](figures/dag.pdf width=48&label=short_distance_graph_2)The Dijkstra's algorithm does not work on graphs with negative weights. So, if we add negative weights to the graph, we must use the Bellman-Fordalgorithm to solve the problem. Figure *@short_distance_graph_3@*![Short distance with negative weights.](figures/longest_path.pdf width=48&label=short_distance_graph_3)If the graph has no cycles, a Directed Acyclic Graph \(DAG\) \(like Figure *@short_distance_graph_3@*\), we can use an algorithm based on topological sort to findthe shortest distance. This algorithm works for both negative and positive weights as long the graph has no cycles. This algorithm is betterin terms of time complexity but is more restricted as it only runs on DAG. On the other hand, Dijkstra's and Bellman-Ford both run in both cyclic and acyclic graphs.### Shortest path on unweighted graphs \(BFS algorithm\)If the graph is unweighted or all edges have the same **non-negative** weight, the shortest path can be foundin linear time $O(V + E)$ using Breadth First Search \(BFS\) algorithm.BFS is an algorithm for traveling a graph in a traversal way. That means that the algorithm will travel the children of the starting node always in orderensuring that when the goal node, if exists, is founded the path will be the shortest one possible.BFS is a single source shortest path algorithm. That means that before running the algorithm it is neededto specify a starting node. Then, the algorithm can tell us the shortest path between the starting nodeand all the other nodes.The algorithm is the following one:```initialize a queue Q
22
mark start node as visited
33
Q.addLast(start)
44
while Q is not empty do:

0 commit comments

Comments
 (0)