Skip to content

Commit 658a4d3

Browse files
authored
Merge pull request #12 from Driolar/master
Revised chapters 1, 2 and 9.
2 parents 6150540 + 78f1df9 commit 658a4d3

File tree

4 files changed

+250
-204
lines changed

4 files changed

+250
-204
lines changed

Chapters/Chapter1/chapter1.md

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,59 @@
1-
## IntroductionGraphs are everywhere and there is well-known algorithms to get the best out of them.In this booklet the most common graph algorithms will be explained along with some case studieswhere these algorithms can be applied. These algorithms are available in the _Pharo AI_ _graph-algorithms_ library available at: [https://github.com/pharo-ai/graph-algorithms](https://github.com/pharo-ai/graph-algorithms)This booklet will describe and implement this algorithms.### Paris metro as graph exampleA typical example is a metro map as the one of Paris as shown in Figure *@paris@*.What we see is that metro lines are connected by some stations for example \(Gare du Nord connects line 5 and 4\) and some stations are real hub where multiple lines meet such as Chatelet.![Newly design paris metro maps.](figures/Paris_Metro_map_1024.pdf width=100&label=paris)When you visit Paris, you are often asking yourself:- what are the different possibilities to go to that place?- are they straight connections?- finding the fastest one?- finding the one with the least stops \(because RER is a fast kind of metro but stopping only in certain places\)- finding the one with the least changes?- what are all the places that I can reach in 5 stops?All such questions can be answered by modeling the metro of Paris as a graph and applying algorithms to it.Several graphs can be represented:- we can have graph with only the shared stations and eliminate the station in between \(this would not be really useful for users because you do not want to only used shared stations\).- we can have a graph with the time between two stations.### About testsTo ensure that the algorithms are working properly, we have implemented several tests for this library. We have a graph fixture in which we have implemented different types of graphs.Each graph is implemented on a class side method and the method has a link to a picture to see the graph visually.Then, in each of the tests for each of the algorithms, we construct a graph and check if the result after running the algorithm is the expected one.#### Outline of the documentIn this little book we will describe some algorithms that can be applied to graphs.We will start with some basic definitions, then in subsequent chapters we will describe the following algorithms:topological sort, shortest path, Kruskal, Tarjan, HITS, ...All the algorithms of the library have the same API to set the nodes and the edges.The edges can be both weighted or unweighted.A detailed explanation of how to use the API will be given on Chapter *@cha:repre@*.### How to installYou can install the library executing the following code snippet:```Metacello new
2-
repository: 'github://pharo-ai/graph-algorithms/src';
3-
baseline: 'AIGraphAlgorithms';
4-
load```
1+
## Introduction
2+
3+
Graphs are everywhere and there is well-known algorithms to get the best out of them.
4+
In this booklet the most common graph algorithms will be explained along with some case studies where these algorithms can be applied. These algorithms are available in the _Pharo AI_ _graph-algorithms_ library available at: [https://github.com/pharo-ai/graph-algorithms](https://github.com/pharo-ai/graph-algorithms).
5+
6+
This booklet will describe and implement thess algorithms.
7+
8+
### Paris metro as graph example
9+
10+
A typical example is a metro map as the one of Paris as shown in Figure *@paris@*.
11+
What we see is that metro lines are connected by some stations for example \(Gare du Nord connects line 5 and 4\) and some (interchange) stations are a hub where multiple lines meet such as Chatelet.
12+
13+
![Newly designed Paris metro maps.](figures/Paris_Metro_map_1024.pdf width=100&label=paris)
14+
15+
When you visit Paris, you are often asking yourself:
16+
17+
- what are the different possibilities to go to that place?
18+
- are they straight connections?
19+
- finding the fastest one?
20+
- finding the one with the least stops \(because RER is a fast kind of metro but stopping only in certain places\)
21+
- finding the one with the least changes?
22+
- what are all the places that I can reach in 5 stops?
23+
24+
All such questions can be answered by modeling the metro of Paris as a graph and applying algorithms to it.
25+
26+
Several graphs can be represented:
27+
28+
- we can have graph with only the interchange stations and eliminate the stations in between \(this would not be really useful for users because you do not want to only use interchange stations\).
29+
- we can have a graph with the ride time between two stations.
30+
31+
### About tests
32+
33+
To ensure that the algorithms are working properly, we have implemented several tests for this library. We have a graph fixture in which we have implemented different types of graphs.
34+
Each graph is implemented on a class side method and the method has a link to a picture to see the graph visually.
35+
Then, in each of the tests for each of the algorithms, we construct a graph and check if the result after running the algorithm is the expected one.
36+
37+
#### Outline of the document
38+
39+
In this little book we will describe some algorithms that can be applied to graphs.
40+
We will start with some basic definitions, then in subsequent chapters we will describe algorithms for several graph problem categories like topological sorting, shortest path, minimum spanning tree, strongly connected components, link analysis, graph matching or maximum flow. The algorithms are often named for its inventor or inventors (Kruskal, Tarjan, Bellman-Ford, ...) or use acronyms (BFS, DAG, HITS, ...).
41+
42+
All the algorithms of the library have the same API to set the nodes and the edges.
43+
The edges can be both weighted or unweighted. A detailed explanation of how to use the API will be given on Chapter *@cha:repre@*.
44+
45+
### How to install
46+
47+
You can install the library executing the following code snippet{!footnote|EpMonitor serves to deactive Epicea, a Pharo code recovering mechanism, during the installation.!}:
48+
49+
```
50+
EpMonitor disableDuring: [
51+
Metacello new
52+
repository: 'github://pharo-ai/graph-algorithms';
53+
baseline: 'AIGraphAlgorithms';
54+
load ]
55+
```
56+
57+
However, the library is provided by a fresh Pharo image since Pharo 11.
58+
59+
You may also separately download from the `GraphGenerators` baseline group a tenner of library algorithms for generating regular and random graphs.

Chapters/Chapter2/chapter2.md

Lines changed: 97 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,97 @@
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.
1+
## Basic Definitions
2+
3+
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.
4+
Graphs are discrete mathematical structures that consist of a set of vertices \(also called nodes\) and a set of edges that connect those vertices.
5+
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.
6+
7+
There are multiple types of graphs according if the edges are directed or undirected, if the edges are weighted or unweighted and so on.
8+
In this chapter, basic graph concepts and graphs types are going to be explained to ease the following of the algorithms.
9+
10+
### Type of Graphs
11+
12+
#### Directed Graph
13+
14+
A directed graph (a *digraph*, for short) 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 be drawn like in Figure *@directed@*.
15+
16+
![A directed graph.](figures/directed_graph.pdf width=30&label=directed)
17+
18+
#### Undirected Graph
19+
20+
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 understood that the graph has no direction if the direction is not specified explicitly as shown in Figure *@undirected@*.
21+
22+
![An undirected graph.](figures/undirected_graph.pdf width=30&label=undirected)
23+
24+
#### Weighted Graph
25+
26+
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.
27+
For example, a graph can be a map in which the nodes represent cities and the edges represent the distance between those cities.
28+
29+
![A weighted graph: edges have a weight.](figures/weighted_graph.pdf width=40&label=weighted)
30+
31+
#### Connected Graph
32+
33+
A connected graph is an undirected graph in which exists a path for every pair of nodes.
34+
For example, Figure *@connected@* represents a connected graph because from any node you can get to any node.
35+
36+
![A connected graph: all the nodes are reachable from any others.](figures/connected_graph.pdf width=40&label=connected)
37+
38+
But, Figure *@directed2@* is a disconnected graph because the nodes F and G are isolated from the rest.
39+
40+
![A disconnected graph: some nodes are not reachable from others.](figures/disconnected_graph.pdf width=48&label=directed2)
41+
42+
#### Bipartite Graph
43+
44+
A bipartite graph is a graph whose set of nodes can be split into two subsets $A$ and $B$ in such a way that each edge of the graph joins a node in $A$ and a node in $B$. In Figure @bipartite@, the two subsets are $\{A1, A2, A3, A4, A5\}$ and $\{B1, B2, B3, B4\}$.
45+
A *complete* bipartite graph is a bipartite graph in which each node in A is joined to each node in B by just one edge.
46+
47+
![A bipartite graph: the nodes are split into two subsets.](figures/bipartite.png width=48&label=bipartite)
48+
49+
### Graph Cycle
50+
51+
A 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 greater than 3.
52+
53+
For example, in Figure *@cycle1@* there is a cycle between nodes _A, B, D_.
54+
55+
![A cycle in a graph.](figures/cycle_in_a_graph.pdf width=40&label=cycle1)
56+
57+
But in Figure *@cycle2@*, there is no cycle from node A to C because the node D has to be traveled twice. Nevertheless, there is a cycle between nodes D, B and C.
58+
59+
![In a directed graph, direction is impacting cycle presence.](figures/not_a_cycle.pdf width=52&label=cycle2)
60+
61+
#### Directed Acyclic Graph \(DAG\)
62+
63+
Like the name suggests, a directed acyclic graph is a directed graph that does not have any cycles \(as shown in Figure *@DAG@*\).
64+
65+
![A Directed Acyclic Graph (DAG).](figures/dag.pdf width=35&label=DAG)
66+
67+
#### Strongly Connected Graph
68+
69+
Unlike the connected graph, a strongly connected graph is a **directed** graph in which there is a path for every pair of nodes. Figure *@strongly1@* is a strongly connected graph.
70+
71+
![A strongly connected graph.](figures/strongly_connected_graph.pdf width=30&label=strongly1)
72+
73+
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 an **undirected** connected graph. For that reason, Figure *@strongly2@* is called a weakly connected graph.
74+
75+
![A weakly connected graph.](figures/not_strongly_connected_graph.pdf width=30&label=strongly2)
76+
77+
#### Strongly Connected Component
78+
79+
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:
80+
$\{A, B, C\}$, $\{F, E\}$, $\{D\}$.
81+
82+
![Graph with three strongly connected components](figures/strongly_connected_components.pdf width=50)
83+
84+
### Tree
85+
86+
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
87+
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 incoming \(parent\) node \(See Figure *@directedTree@*\). If you remove the direction of the directed acyclic graph, the remaining graph has to be an **undirected** tree \(See Figure *@undiTree@*\).
88+
89+
![A tree: a connected graph without cycles.](figures/tree.pdf width=40&label=undiTree)
90+
91+
![A directed tree.](figures/directed_tree.pdf width=40&label=directedTree)
92+
93+
### Conclusion
94+
95+
These definitions set the stage for the algorithms that we will now describe.
96+
Identifying clearly the kind of graphs an algorithm is applied on is key because the working hypotheses
97+
are really important.
22.3 KB
Loading

0 commit comments

Comments
 (0)