Skip to content

Commit 8772536

Browse files
authored
Merge pull request #4 from Driolar/master
Added new chapter about Matchings (Independent Edge Sets)
2 parents 8709f68 + 8b1724d commit 8772536

File tree

5 files changed

+200
-1
lines changed

5 files changed

+200
-1
lines changed

Chapters/Chapter2/chapter2.md

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,114 @@
1-
## Basic definitionsThere is a great set of mathematical problems that can be solved using graph models. Graphs are vastly used in all kind of computer science problems.Graphs are discrete mathematical structures that consist of a set of vertices \(also called nodes\) and a set of edges that connect those vertices.In computer science is more commonly used the term _node_ instead of _vertex_. In this booklet the term node is going to be the one used.There are multiple types of graphs according if the edges are directed or undirected, if the edges are weighted or unweighted and so on.In this chapter, basic graph concepts and graphs types are going to be explained to ease the following of the algorithms.### Type of Graphs#### Directed GraphA directed graph is a type of graph in which every edges has a direction: an ingoing node and an outgoing node. The most commonly way of representing a graph is to draw it.A directed graph can de drawn like in Figure *@directed@*:![A directed graph.](figures/directed_graph.pdf width=30&label=directed)#### Undirected GraphAn undirected graph is a type of graph in which the edges does not have a direction. They can be drawn without a line that does not have any arrow heads. It isunderstood that the graph has no direction if the direction is not specified explicitly as shown in Figure *@undirected@*.![An undirected graph.](figures/undirected_graph.pdf width=30&label=undirected)#### Weighted GraphA weighted graph is a graph that each of its edges has an associated weight \(see Figure *@weighted@*\). In real life examples, the weights can represent several things.For example, a graph can be a map in which the nodes represent cities and the edges represent the distance between those cities.![A weighted graph: edges have a weight.](figures/weighted_graph.pdf width=40&label=weighted)#### Connected GraphA connected graph is an undirected graph in which exists a path for every pair of nodes.For example, Figure *@connected@* represents a connected graph because from any node you can get to any node.![A connected graph: all the nodes are reachable from any others.](figures/connected_graph.pdf width=40&label=connected)But, Figure *@directed2@* is a disconnected graph because the nodes F and G are isolated from the rest.![A disconnected graph: some nodes are not reachable from others.](figures/disconnected_graph.pdf width=48&label=directed2)### Graph CycleA graph cycle is a sequence of adjacent nodes in which all nodes are different except of the first and the last one.That means, a graph cycle is a path that ends and starts in the same node without repeating any other node and it has a size grater than 3.For example, in Figure *@cycle1@* there is a cycle between nodes _A, B, D_.![A Cycle in a graph.](figures/cycle_in_a_graph.pdf width=40&label=cycle1)But, in Figure *@cycle2@* there is no cycle between from node A to C because the node D has to be traveled twice.Nevertheless, there is a cycle between node D, B and C.![In a directed graph, direction is impacting cycle presence. ](figures/not_a_cycle.pdf width=52&label=cycle2)#### Directed Acyclic Graph \(DAG\)Like the name suggests, a directed acyclic graph is a directed graph that does not have any cycles \(as shown in Figure *@DAG@*\).![A directed Acyclic Graph.](figures/dag.pdf width=35&label=DAG)#### Strongly Connected GraphUnlike the Connected Graph, a Strongly Connect is a **directed** graph in which there is a path for every pair of nodes. Figure *@strongly1@* is a strongly connected graph.![A strongly connected graph.](figures/strongly_connected_graph.pdf width=30&label=strongly1)Figure *@strongly2@* is not strongly connected because it is not possible to reach node D from node A. However, if the directions of the graph are deleted, the graph becomes a**undirected** connected graph. For that reason, Figure *@strongly2@* is called a weakly connected graph.![A weakly connected graph.](figures/not_strongly_connected_graph.pdf width=30&label=strongly2)#### Strongly Connected ComponentA strongly connected component of a directed graph is the maximal subgraph that is strongly connected. In the figure there are three strongly connected components in the graph: $\{A, B, C\}$, $\{F, E\}$, $\{D\}$.![Graph with three strongly connected components](figures/strongly_connected_components.pdf width=50)### TreeA tree is a connected graph without cycles. That means that there is only one path between every pair of vertices. But, in the computer science context, normally a tree isrepresented as a **directed** graph. In that case, the definition will be that a **directed** tree is a directed acyclic graph in which every node has only oneincoming \(parent\) node \(See Figure *@directedTree@*\). If you remove the direction of the directed acyclic graph the reaming graph has to be an **undirected** tree \(See Figure *@undiTree@*\).![A tree: a connected graph without cycles.](figures/tree.pdf width=40&label=undiTree)![A directed tree.](figures/directed_tree.pdf width=40&label=directedTree)### ConclusionThese definitions set the stage for the algorithms that we will now describe.Identifying clearly the kind of graphs an algorithm is applied on is key because the working hypothesesare really important.
1+
## Basic definitions
2+
3+
4+
There is a great set of mathematical problems that can be solved using graph models. Graphs are vastly used in all kind of computer science problems.
5+
Graphs are discrete mathematical structures that consist of a set of vertices \(also called nodes\) and a set of edges that connect those vertices.
6+
In computer science is more commonly used the term _node_ instead of _vertex_. In this booklet the term node is going to be the one used.
7+
8+
There are multiple types of graphs according if the edges are directed or undirected, if the edges are weighted or unweighted and so on.
9+
In this chapter, basic graph concepts and graphs types are going to be explained to ease the following of the algorithms.
10+
11+
### Type of Graphs
12+
13+
14+
#### Directed Graph
15+
16+
17+
A directed graph is a type of graph in which every edges has a direction: an ingoing node and an outgoing node. The most commonly way of representing a graph is to draw it.
18+
A directed graph can de drawn like in Figure *@directed@*:
19+
20+
![A directed graph.](figures/directed_graph.pdf width=30&label=directed)
21+
22+
#### Undirected Graph
23+
24+
25+
An undirected graph is a type of graph in which the edges does not have a direction. They can be drawn without a line that does not have any arrow heads. It is
26+
understood that the graph has no direction if the direction is not specified explicitly as shown in Figure *@undirected@*.
27+
28+
![An undirected graph.](figures/undirected_graph.pdf width=30&label=undirected)
29+
30+
#### Weighted Graph
31+
32+
33+
A weighted graph is a graph that each of its edges has an associated weight \(see Figure *@weighted@*\). In real life examples, the weights can represent several things.
34+
For example, a graph can be a map in which the nodes represent cities and the edges represent the distance between those cities.
35+
36+
![A weighted graph: edges have a weight.](figures/weighted_graph.pdf width=40&label=weighted)
37+
38+
#### Connected Graph
39+
40+
41+
A connected graph is an undirected graph in which exists a path for every pair of nodes.
42+
For example, Figure *@connected@* represents a connected graph because from any node you can get to any node.
43+
44+
![A connected graph: all the nodes are reachable from any others.](figures/connected_graph.pdf width=40&label=connected)
45+
46+
But, Figure *@directed2@* is a disconnected graph because the nodes F and G are isolated from the rest.
47+
48+
![A disconnected graph: some nodes are not reachable from others.](figures/disconnected_graph.pdf width=48&label=directed2)
49+
50+
#### Bipartite Graph
51+
52+
A bipartite graph is a graph whose set of vertices can be split into two subsets A and B in such a way that each edge of the graph joins a vertex in A and a vertex in B.
53+
A complete bipartite graph is a bipartite graph in which each vertex in A is joined to each vertex in B by just one edge.
54+
55+
56+
### Graph Cycle
57+
58+
59+
A graph cycle is a sequence of adjacent nodes in which all nodes are different except of the first and the last one.
60+
That means, a graph cycle is a path that ends and starts in the same node without repeating any other node and it has a size grater than 3.
61+
62+
For example, in Figure *@cycle1@* there is a cycle between nodes _A, B, D_.
63+
64+
![A Cycle in a graph.](figures/cycle_in_a_graph.pdf width=40&label=cycle1)
65+
66+
But, in Figure *@cycle2@* there is no cycle between from node A to C because the node D has to be traveled twice.
67+
Nevertheless, there is a cycle between node D, B and C.
68+
69+
![In a directed graph, direction is impacting cycle presence. ](figures/not_a_cycle.pdf width=52&label=cycle2)
70+
71+
#### Directed Acyclic Graph \(DAG\)
72+
73+
74+
Like the name suggests, a directed acyclic graph is a directed graph that does not have any cycles \(as shown in Figure *@DAG@*\).
75+
76+
![A directed Acyclic Graph.](figures/dag.pdf width=35&label=DAG)
77+
78+
#### Strongly Connected Graph
79+
80+
81+
Unlike the Connected Graph, a Strongly Connect is a **directed** graph in which there is a path for every pair of nodes. Figure *@strongly1@* is a strongly connected graph.
82+
83+
![A strongly connected graph.](figures/strongly_connected_graph.pdf width=30&label=strongly1)
84+
85+
Figure *@strongly2@* is not strongly connected because it is not possible to reach node D from node A. However, if the directions of the graph are deleted, the graph becomes a
86+
**undirected** connected graph. For that reason, Figure *@strongly2@* is called a weakly connected graph.
87+
88+
![A weakly connected graph.](figures/not_strongly_connected_graph.pdf width=30&label=strongly2)
89+
90+
#### Strongly Connected Component
91+
92+
93+
A strongly connected component of a directed graph is the maximal subgraph that is strongly connected. In the figure there are three strongly connected components in the graph:
94+
$\{A, B, C\}$, $\{F, E\}$, $\{D\}$.
95+
96+
![Graph with three strongly connected components](figures/strongly_connected_components.pdf width=50)
97+
98+
### Tree
99+
100+
101+
A tree is a connected graph without cycles. That means that there is only one path between every pair of vertices. But, in the computer science context, normally a tree is
102+
represented as a **directed** graph. In that case, the definition will be that a **directed** tree is a directed acyclic graph in which every node has only one
103+
incoming \(parent\) node \(See Figure *@directedTree@*\). If you remove the direction of the directed acyclic graph the reaming graph has to be an **undirected** tree \(See Figure *@undiTree@*\).
104+
105+
![A tree: a connected graph without cycles.](figures/tree.pdf width=40&label=undiTree)
106+
107+
![A directed tree.](figures/directed_tree.pdf width=40&label=directedTree)
108+
109+
### Conclusion
110+
111+
112+
These definitions set the stage for the algorithms that we will now describe.
113+
Identifying clearly the kind of graphs an algorithm is applied on is key because the working hypotheses
114+
are really important.

Chapters/chapter9/chapter9.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
## Matchings (Independent Edge Sets)
2+
In graph theory, a matching or independent edge set in an undirected graph is a set of edges without common vertices.
3+
The term matching is the more popular one, but it shouldn't be confused with another meaning of graph matching, namely computing the similarity of graphs (graph isomorphism).
4+
5+
Matchings are used in various applications such as network design, job assignments, and scheduling.
6+
More specifically, matching strategies are very useful in flow network algorithms such as the Edmonds-Karp algorithm that is also in our library.
7+
8+
### 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).
10+
11+
![Examples of maximal matchings.](figures/Maximal-matching.pdf width=50&label=maximal)
12+
13+
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.
15+
16+
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.
18+
19+
![Examples of maximum matchings.](figures/Maximum-matching.pdf width=50&label=maximum)
20+
21+
We note from these examples that a maximum matching is always maximal, but the converse does not always hold.
22+
23+
A **maximum-weight** matching in a weighted graph is a matching with the largest sum of weights.
24+
Similarly, a **minimum-weight** matching has the smallest sum of weights.
25+
26+
### A greedy algorithm for matching
27+
The algorithm for graph matching implemented in this library is a simple greedy algorithm with time complexity of $O(|E|log(|V|))$.
28+
The class ```AIGraphMatchingAlgorithm``` can be instantiated to find an **approximation** either of the maximum-weight, the minimum-weight or the maximum-cardinality matching.
29+
30+
For instance, the pseudocode to greedily find a matching of large weight is the following one:
31+
```
32+
M ← Empty list that will contain the matching edges
33+
E ← Set of all edges
34+
remove all loops (edges joining a vertex to itself) from E
35+
36+
while E is not empty do
37+
let e be the edge in E with the highest weight
38+
add e to M
39+
remove e and all edges incident to e from E
40+
```
41+
For finding a matching of small weight, we use exactly the same algorithm except that we have to order the edges in descending weight instead of ascending.
42+
This is the same principle we already met for the Kruskal's algorithm.
43+
44+
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.
45+
Nevertheless, it can be proven that it is a 2-approximation (greedy result >= 1/2 optimal result).
46+
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@.
48+
49+
![Counterexample: The greedy algorithm is not optimal.](figures/epsilon.pdf width=50&label=epsilon)
50+
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).
52+
53+
### Stable matchings
54+
Maximality and minimality are not the only interesting optimal matching problems.
55+
Another goal for optimal matching is stability, based on mutual preferences between two contender groups such as men and women or students and colleges.
56+
57+
Our library implements the classical Gale-Shapely algorithm that solves the important Stable Matching Problem (aka Stable Marriage Problem).
58+
To simplify, the problem considers two groups A and B of equal size n and conceives a complete **bipartite** graph with their possible relations.
59+
Each group member has defined a strict preference ordering over all the members of the other group.
60+
A resulting matching would contain n edges, each one relating a different pair.
61+
62+
A matching is **stable** when there does not exist any pair which both prefer each other to their current partner under the matching.
63+
This leads to a sense of harmony and fairness in the outcome.
64+
65+
Here is the algorithm's pseudocode:
66+
```
67+
Every vertex begins unmatched.
68+
While there is an unmatched vertex in the set A
69+
the next unmatched vertex in A proposes to match with its most preferred B vertex it has not already tried to match with
70+
if the B vertex was unmatched then these vertices are now matched
71+
if the B vertex was matched, it chooses its preferred vertex among the proposed one or the existing match;
72+
depending on the choice, the proposed match wins or vertex A remains unmatched
73+
```
74+
This algorithm with complexity $O(n²)$ finds in any case a stable matching. However, it is extreme and dual in the sense that it generally yields the matching that is best for group A among all stable matchings, and worst (but still stable) for group B.
75+
76+
To run ```AIStableMatchingAlgorithm```, both groups of equal size must be set with the complete preferences of each contender ```AIStableMatchingNode``` in order to obtain the stable matching, a set of ```AIStableMatchingEdge```.
77+
78+
The Stable Matching Problem has extensions in many ways, e.g. with groups of different size or incomplete preference lists.
79+
### Conclusion
80+
Graph matching is a challenging problem, especially in large and complex graphs where scalability and complexity are crucial.
81+
Many graph matching algorithms exist in order to optimize for the parameters necessary dictated by the problem at hand.
82+
Depending on the context, approximation algorithms may be a suitable alternative.
83+
84+
From the practical and theoretical points of view, envisaging the special case of bipartite graphs is very common and fructiferous, notably for matching problems.
85+
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.
1.93 KB
Binary file not shown.
1.93 KB
Binary file not shown.
14.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)