Skip to content

Commit 5cc8c6a

Browse files
authored
Merge pull request #5 from Driolar/master
Chapter 9 (Matchings) finished.
2 parents 8772536 + 65406cd commit 5cc8c6a

File tree

6 files changed

+10
-10
lines changed

6 files changed

+10
-10
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:
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ Matchings are used in various applications such as network design, job assignmen
66
More specifically, matching strategies are very useful in flow network algorithms such as the Edmonds-Karp algorithm that is also in our library.
77

88
### Some definitions and small examples
9-
The **cardinality** of a matching is the number of its edges. Figure @maximal@ shows 3 matchings of cardinality 1, 2 and 2 (from left to right).
9+
The **cardinality** of a matching is the number of its edges. Figure *@maximal@* shows 3 matchings of cardinality 1, 2 and 2 (from left to right).
1010

11-
![Examples of maximal matchings.](figures/Maximal-matching.pdf width=50&label=maximal)
11+
![Examples of maximal matchings. (Source: Wikimedia)](figures/Maximal-matching.pdf width=50&label=maximal)
1212

1313
A matching is **maximal** if it cannot be expanded to another matching by addition of any edge in the graph.
14-
The matchings in figure @maximal@ are maximal.
14+
The matchings in Figure *@maximal@* are maximal.
1515

1616
Moreover, a **maximum**(-cardinality) matching has the largest possible cardinality among all matchings in a graph.
17-
For each of the matchings in figure @maximal@, figure @maximum@ shows a maximum matching example with cardinality 2, 3 and 2 respectively.
17+
For each of the matchings in Figure *@maximal@*, Figure *@maximum@* shows a maximum matching example with cardinality 2, 3 and 2 respectively.
1818

19-
![Examples of maximum matchings.](figures/Maximum-matching.pdf width=50&label=maximum)
19+
![Examples of maximum matchings. (Source: Wikimedia)](figures/Maximum-matching.pdf width=50&label=maximum)
2020

2121
We note from these examples that a maximum matching is always maximal, but the converse does not always hold.
2222

@@ -44,11 +44,11 @@ This is the same principle we already met for the Kruskal's algorithm.
4444
In any case, the greedy graph matching algorithm finds a maximal matching but it doesn't always find the optimal solution, in contrast to more expensive matching algorithms like the Hungarian Maximum Matching Algorithm, the Blossom Algorithm or the Hopcroft–Karp Algorithm.
4545
Nevertheless, it can be proven that it is a 2-approximation (greedy result >= 1/2 optimal result).
4646

47-
To see that the result is not always optimal, consider the two matching versions of the weighted graph with four nodes and three edges in figure @epsilon@.
47+
To see that the result is not always optimal, consider the two matching versions of the weighted graph with four nodes and three edges in Figure *@epsilon@*.
4848

4949
![Counterexample: The greedy algorithm is not optimal.](figures/epsilon.pdf width=50&label=epsilon)
5050

51-
The greedy algorithm for maximum-weight would first take weight 1+epsilon and then stop (figure @epsilon@ top), missing the optimal weight sum 1+1 (figure @epsilon@ bottom).
51+
The greedy algorithm for maximum-weight would first take weight 1+epsilon and then stop (Figure *@epsilon@* top), missing the optimal weight sum 1+1 (Figure *@epsilon@* bottom).
5252

5353
### Stable matchings
5454
Maximality and minimality are not the only interesting optimal matching problems.
@@ -83,4 +83,4 @@ Depending on the context, approximation algorithms may be a suitable alternative
8383

8484
From the practical and theoretical points of view, envisaging the special case of bipartite graphs is very common and fructiferous, notably for matching problems.
8585

86-
Both the greedy and the stable matching algorithm presented here are further examples of using a working list until emptied, like discussed for the topological sorting.
86+
Both the greedy and the stable matching algorithm presented here are further examples of using a working list until emptied, like discussed for the topological sorting in this book.
File renamed without changes.
File renamed without changes.

index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<!inputFile|path=Chapters/Chapter1/chapter1.md!><!inputFile|path=Chapters/Chapter2/chapter2.md!><!inputFile|path=Chapters/Chapter3/chapter3.md!><!inputFile|path=Chapters/Chapter4/chapter4.md!><!inputFile|path=Chapters/Chapter5/chapter5.md!><!inputFile|path=Chapters/Chapter6/chapter6.md!><!inputFile|path=Chapters/Chapter7/chapter7.md!><!inputFile|path=Chapters/Chapter8/chapter8.md!>
1+
<!inputFile|path=Chapters/Chapter1/chapter1.md!><!inputFile|path=Chapters/Chapter2/chapter2.md!><!inputFile|path=Chapters/Chapter3/chapter3.md!><!inputFile|path=Chapters/Chapter4/chapter4.md!><!inputFile|path=Chapters/Chapter5/chapter5.md!><!inputFile|path=Chapters/Chapter6/chapter6.md!><!inputFile|path=Chapters/Chapter7/chapter7.md!><!inputFile|path=Chapters/Chapter8/chapter8.md!><!inputFile|path=Chapters/Chapter9/chapter9.md!>

0 commit comments

Comments
 (0)