-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBreadthFirstSearch.sol
More file actions
96 lines (77 loc) · 3.35 KB
/
Copy pathBreadthFirstSearch.sol
File metadata and controls
96 lines (77 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.19;
import {Counters} from "@openzeppelin/contracts/utils/Counters.sol";
/**
* @title Breadth-First Search
* @notice The contract implements a search() function that performs breadth-first search.
* @dev To organize the search, you need to create a graph.
* The addNode() function creates nodes in the graph.
* The addEdge() function creates edges in the graph (connections between nodes).
* Important! All nodes in the graph are numbered in the order they are added.
* This is necessary to know the total number of nodes for conducting the search and keeping track of visited nodes.
*/
contract BreadthFirstSearch {
using Counters for Counters.Counter;
struct Node {
string name;
uint256[] neighbors;
}
mapping(uint256 => Node) private _graph;
Counters.Counter private _nodeCount;
/**
* @notice Creates nodes in the graph
* @param name The name of the node
*/
function addNode(string memory name) external {
Node storage newNode = _graph[_nodeCount.current()];
newNode.name = name;
_nodeCount.increment();
}
/**
* @notice Adds a connection between nodes in the graph
* @param from The starting node of the connection
* @param to The ending node of the connection
*/
function addEdge(uint256 from, uint256 to) external {
_graph[from].neighbors.push(to);
}
/**
* @notice Invokes the breadth-first search algorithm
* @param start The identifier of the node to start the search from
* @param goal The identifier of the node to find
* @dev Essentially, this function checks the possibility of reaching one node in the graph from another
*/
function search(uint256 start, uint256 goal) external view returns (bool) {
/// An array to keep track of visited nodes. This helps to prevent cycling
bool[] memory visited = new bool[](_nodeCount.current());
/// An array to organize the search queue.
/// As we check each node, we will add the connections of this node to the end of the queue for later examination.
uint256[] memory queue = new uint256[](_nodeCount.current());
/// Counters for navigating through the queue array. They will help in adding nodes to the queue and extracting them from the queue.
uint256 front = 0;
uint256 back = 0;
/// Place the initial element in the queue
queue[back++] = start;
/// Mark the node as visited
visited[start] = true;
while (front != back) {
uint256 current = queue[front++];
if (current == goal) {
/// If the target value equals the value of the graph node, the value is found
return true;
}
/// Extract all neighboring nodes of the examined graph node
uint256[] memory neighbors = _graph[current].neighbors;
for (uint256 i = 0; i < neighbors.length; ++i) {
uint256 neighbor = neighbors[i];
if (!visited[neighbor]) {
/// Mark the neighboring node as checked
visited[neighbor] = true;
/// Add the neighboring node to the queue
queue[back++] = neighbor;
}
}
}
return false;
}
}