You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/algorithms/10-graphs/README.md
+180-5Lines changed: 180 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,8 @@
1
1
# Graph
2
2
3
-
Graphs are a type of data structures that interconnects nodes (or vertices) with edges. They are used to model relationships between objects.
3
+
Graphs are a type of data structures that interconnects nodes (or vertices) with edges. They are used to model relationships between objects. This is the basics for most AI algorithms, such as pathfinding, decision making, neuron networks, and others.
4
+
5
+

4
6
5
7
## Basic Definitions
6
8
@@ -10,6 +12,8 @@ Graphs are a type of data structures that interconnects nodes (or vertices) with
10
12
-**Path** is the sequence of edges and nodes that allows you to go from one node to another.
11
13
-**Degree** of a node is the number of edges connected to it.
12
14
15
+

16
+
13
17
## Representation
14
18
15
19
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.
@@ -75,18 +79,189 @@ vector<Node> nodes;
75
79
-**Complete graph**: A graph where every pair of nodes is connected by a unique edge.
76
80
-**Regular graph**: A graph where every node has the same degree.
77
81
82
+

83
+
78
84
## Graph Algorithms
79
85
80
86
### Depth-First Search (DFS)
81
87
82
88
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.
// it exploits the call stack to store the nodes to visit
105
+
// you might want to use the iterative version if you have a large graph
106
+
// for that, use std::stack data structure and producer-consumer pattern
107
+
voiddfs(const std::string& node) {
108
+
std::cout << node << std::endl;
109
+
visited.insert(node);
110
+
for (const auto& neighbor : graph[node])
111
+
if (!visited.contains(neighbor))
112
+
dfs(neighbor);
113
+
}
114
+
115
+
void dfs_interactive(const std::string& node) {
116
+
std::stack<std::string> stack;
117
+
// produce the first node
118
+
stack.push(node);
119
+
while (!stack.empty()) {
120
+
// consume the node
121
+
std::string current = stack.top();
122
+
stack.pop();
123
+
// avoid visiting the same node twice
124
+
if (visited.contains(current))
125
+
continue;
126
+
// mark as visited
127
+
visited.insert(current);
128
+
129
+
// visit the node
130
+
std::cout << current << std::endl;
131
+
132
+
// produce the next node to visit
133
+
for (const auto& neighbor : graph[current]) {
134
+
if (!visited.contains(neighbor)) {
135
+
stack.push(neighbor);
136
+
break; // is this break necessary?
137
+
}
138
+
}
139
+
}
140
+
}
141
+
142
+
int main() {
143
+
std::cout << "Write one node string per line. When you are done, add an empty line." << std::endl;
144
+
std::string node;
145
+
while (std::getline(std::cin, node) && !node.empty())
146
+
graph[node] = {};
147
+
std::cout << "Write the edges as 'node1;node2'. When you are done, add an empty line." << std::endl;
148
+
std::string edge;
149
+
while (std::getline(std::cin, edge) && !edge.empty()) {
150
+
auto pos = edge.find(';');
151
+
// Bidirectional
152
+
std::string source = edge.substr(0, pos);
153
+
std::string destination = edge.substr(pos + 1);
154
+
graph[source].insert(destination);
155
+
graph[destination].insert(source);
156
+
}
157
+
std::cout << "Write the starting node." << std::endl;
158
+
std::string start;
159
+
std::cin >> start;
160
+
dfs(start);
161
+
return 0;
162
+
}
163
+
```
86
164
87
165
### Breadth-First Search (BFS)
88
166
89
167
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.
0 commit comments