Skip to content

Commit 59c3ed5

Browse files
committed
fix: add graphs - wip
1 parent e614f7d commit 59c3ed5

6 files changed

Lines changed: 103 additions & 7 deletions

File tree

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Graph
2+
3+
Graphs are a type of data structures that interconnects nodes (or vertices) with edges. They are used to model relationships between objects.
4+
5+
## Basic Definitions
6+
7+
- **Nodes or vertices** are the basic entities in a graph and hold the data.
8+
- **Edges** are the connections and relation between the nodes. The relationship can be enriched in multiple ways such as direction, weight, and others.
9+
- **Neighbours** are the nodes that are connected to a specific node.
10+
- **Path** is the sequence of edges and nodes that allows you to go from one node to another.
11+
- **Degree** of a node is the number of edges connected to it.
12+
13+
## Representation
14+
15+
A graph is composed by a set of vertices(nodes) and edges. There are multiple ways to represent a graph, and every style has its own advantages and disadvantages.
16+
17+
### Adjacency matrix
18+
19+
Assuming every node is labeled with a number from `0` to `n-1`, an adjacency matrix is a 2D array of size `n` x `n`. The entry `a[i][j]` is 1 if there is an edge from node i to node j, and 0 otherwise. The adjacency matrix for a graph is always a square matrix.
20+
21+
```c++
22+
// adjacency matrix
23+
// NUMBER_OF_NODES is the number of nodes
24+
// bool marks if there is an edge between the nodes.
25+
// switch bool to float if you want to store the weight of the edge.
26+
// switch bool to a data structure if you want to store more information about the edge.
27+
bool adj_matrix[NUMBER_OF_NODES][NUMBER_OF_NODES];
28+
vector<Node> nodes;
29+
```
30+
31+
- **Pros**: it is simple and easy to implement and blazing fast for checking if there is an edge between two nodes.
32+
- **Cons**: it consumes a lot of space, especially for sparse graphs.
33+
34+
### Adjacency list
35+
36+
It can be implemented in multiple ways, but a common one is to use an array of lists(or vectors). The index(key) of the array is the node id, and the value is a list of nodes that are connected to the key node.
37+
38+
```c++
39+
// adjacency list
40+
// NUMBER_OF_NODES is the number of nodes
41+
// vector for storing the connected nodes ids as integers
42+
// switch vector<int> to map<int, float> if you want to store the weight of the edge.
43+
// switch map<int, float> to map<int, data_structure> if you want to store more information about the edge.
44+
vector<int> adj_list[NUMBER_OF_NODES];
45+
vector<Node> nodes;
46+
```
47+
48+
- **Pros**: it is more memory efficient for sparse graphs.
49+
- **Cons**: it can be slower to check if there is an edge between two nodes.
50+
51+
### Edge list
52+
53+
It is a collection of edges, where each egge can be represented as a pair of nodes, a pair of node ids, or a pair of references to nodes.
54+
55+
```c++
56+
// edge list
57+
vector<pair<int, int>> edges;
58+
vector<Node> nodes;
59+
```
60+
61+
- **Pros**: it is the most memory efficient representation for sparse graphs.
62+
- **Cons**: it can be slower to check if there is an edge between two nodes.
63+
64+
## Graph Types
65+
66+
- **Null graph**: A graph with no edges.
67+
- **Trivial graph**: A graph with only one vertex.
68+
- **Directed graph**: A graph where the edges have direction.
69+
- **Weighted graph**: A graph where the edges have a weight.
70+
- **Undirected graph**: A graph where the edges have no direction or are bidirectional. If weighted, the weights are the same in both directions.
71+
- **Connected graph**: A graph where all nodes can be reached from any other node.
72+
- **Disconnected graph**: A graph where some nodes cannot be reached from other nodes.
73+
- **Cyclic graph**: A graph that has at least one cycle, a path that starts and ends at the same node.
74+
- **Acyclic graph**: A graph that has no cycles.
75+
- **Complete graph**: A graph where every pair of nodes is connected by a unique edge.
76+
- **Regular graph**: A graph where every node has the same degree.
77+
78+
## Graph Algorithms
79+
80+
### Depth-First Search (DFS)
81+
82+
DFS is a graph traversal algorithm based on a stack data structure. Basically, the algorithm starts at a node and explores as far as possible along each branch before backtracking. It is used to find connected components, determine the connectivity of the graph, and solve many other problems.
83+
84+
<iframe scrolling="no" style="position:relative;top:0px;width:100%;height:500px;" src="https://www.cs.usfca.edu/~galles/visualization/DFS.html"></iframe>
85+
<a href="https://www.cs.usfca.edu/~galles/visualization/DFS.html">source</a>
86+
87+
### Breadth-First Search (BFS)
88+
89+
BFS is a graph traversal algorithm based on a queue data structure. It starts at a node and explores all of its neighbours before moving on to the next level of neighbours by enqueing them. It is used to find the shortest path, determine the connectivity of the graph, and others.
90+
91+
<iframe scrolling="no" style="position:relative;top:0px;width:100%;height:500px;" src="https://www.cs.usfca.edu/~galles/visualization/BFS.html"></iframe>
92+
<a href="https://www.cs.usfca.edu/~galles/visualization/BFS.html">source</a>
-806 KB
Binary file not shown.

docs/artificialintelligence/09-minmax/README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@ Min-Max algorithms shines in places where you will have to maximize the gain and
88

99
## Algorithm
1010

11-
![min-max.gif](min-max.gif) [source](https://www.youtube.com/watch?v=l-hh51ncgDI)
11+
## Alpha beta prunning
1212

13-
[//]: # (![img.png]&#40;img.png&#41; [source]&#40;https://en.wikipedia.org/wiki/File:AB_pruning.svg&#41;)
13+
### Alpha
1414

15+
- Alpha is the best value that the maximizer currently can guarantee at that level or above.
16+
- It is the lower bound that a MAX node can be assigned.
17+
- MAX node will only update the value of alpha if it finds a value greater than alpha.
18+
- Starts at -∞.
1519

16-
[//]: # (## Alpha beta pruning:)
20+
### Beta
1721

18-
[//]: # ()
19-
[//]: # (![Minmaxab.gif]&#40;Minmaxab.gif&#41; [source]&#40;https://upload.wikimedia.org/wikipedia/en/7/79/Minmaxab.gif)
20-
21-
[//]: # ()
22+
- Beta is the best value that the minimizer currently can guarantee at that level or above.
23+
- It is the upper bound that a MIN node can be assigned.
24+
- MIN node will only update the value of beta if it finds a value less than beta.
25+
- Starts at +∞.
-73.4 KB
Binary file not shown.
-40.5 KB
Binary file not shown.
-3.79 MB
Binary file not shown.

0 commit comments

Comments
 (0)