bfs_and_dfs_implementation_v2
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
bfs_and_dfs_implementation_v2 [2023/11/09 17:39] – [General Functions] fpalustre | bfs_and_dfs_implementation_v2 [2023/11/13 09:56] (current) – fpalustre | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== BFS & DFS Implementation | + | ====== BFS & DFS Implementation |
**Author:** Francis Palustre Email: < | **Author:** Francis Palustre Email: < | ||
\\ | \\ | ||
- | **Date:** Last modified on <11/09/23> | + | **Date:** Last modified on <11/13/23> |
\\ | \\ | ||
- | **Keywords: | + | **Keywords: |
\\ | \\ | ||
{{ : | {{ : | ||
\\ | \\ | ||
- | The photo above depicts | + | The photo above depicts the environment used for travel |
\\ | \\ | ||
===== Motivation and Audience ===== | ===== Motivation and Audience ===== | ||
- | This tutorial' | + | This tutorial' |
<fc blue> | <fc blue> | ||
Line 21: | Line 21: | ||
* Know how to navigate within the terminal in Linux | * Know how to navigate within the terminal in Linux | ||
\\ | \\ | ||
- | * This tutorial may also attract readers who are interesting | + | * This tutorial may also attract readers who are interested |
</fc> | </fc> | ||
\\ \\ | \\ \\ | ||
Line 57: | Line 57: | ||
==== General Functions ==== | ==== General Functions ==== | ||
General functions are functions that are found in both files of BFS and DFS | General functions are functions that are found in both files of BFS and DFS | ||
- | \\ | ||
\\ | \\ | ||
* getCurrentTime() gets the current time in UTC in seconds. It is used to provide the time needed for a movement for Robotino. | * getCurrentTime() gets the current time in UTC in seconds. It is used to provide the time needed for a movement for Robotino. | ||
Line 64: | Line 63: | ||
| | ||
| | ||
- | | + | |
+ | // | ||
+ | return (tv.tv_sec) + tv.tv_usec / 1000000.0; | ||
+ | } | ||
+ | |||
+ | * roboMove() prints the final path from the algorithm and will result into the movement of Robotino | ||
+ | |||
+ | void roboMove(map< | ||
+ | | ||
+ | |||
+ | // | ||
+ | for (vector< | ||
+ | cout << *it << " "; | ||
+ | } cout << endl; | ||
+ | |||
+ | // | ||
+ | for (int i = 0; i < int(roboPath.size()) - 1; i++) { | ||
+ | // | ||
+ | int currPos = atoi(roboPath.at(i).c_str()); | ||
+ | int nextPos = atoi(roboPath.at(i + 1).c_str()); | ||
+ | |||
+ | if ((currPos + 6) == nextPos) { | ||
+ | moveY(0, | ||
+ | } | ||
+ | |||
+ | if ((currPos - 6) == nextPos) { | ||
+ | moveY(1, | ||
+ | } | ||
+ | |||
+ | if ((currPos - 1) == nextPos) { | ||
+ | moveX(1, | ||
+ | } | ||
+ | |||
+ | if ((currPos + 1) == nextPos) { | ||
+ | moveX(0, | ||
+ | } | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | |||
+ | * removeEdges() removes the edges of selected nodes. Can be used to represent obstacles/ | ||
+ | |||
+ | map< | ||
+ | for (vector< | ||
+ | | ||
+ | |||
+ | for (vector< | ||
+ | for (vector< | ||
+ | |||
+ | // | ||
+ | | ||
+ | |||
+ | | ||
+ | } | ||
+ | | ||
+ | } | ||
} | } | ||
==== Robotino Functions ==== | ==== Robotino Functions ==== | ||
Robotino functions are functions that used for Robotino' | Robotino functions are functions that used for Robotino' | ||
+ | |||
+ | * moveX() is the movement in the horizontal direction | ||
+ | |||
+ | void moveX(int first, int second) { | ||
+ | int checker = first - second; // | ||
+ | int distance = abs(checker); | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | for (int i =0; i < distance; | ||
+ | if (checker > 0) { //if positive... | ||
+ | | ||
+ | do { | ||
+ | | ||
+ | | ||
+ | } while (elapsed_time < end_time); | ||
+ | } else { //if negative... | ||
+ | | ||
+ | do { | ||
+ | | ||
+ | | ||
+ | } while (elapsed_time < end_time); | ||
+ | } | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | |||
+ | * moveY() is the movement in the vertical direction | ||
+ | |||
+ | void moveY(int first, int second) { //same comments applied to this function | ||
+ | int checker = first - second; | ||
+ | int distance = abs(checker); | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | for (int i =0; i < distance; | ||
+ | if (checker > 0) { //if positive... | ||
+ | | ||
+ | do { | ||
+ | | ||
+ | | ||
+ | } while (elapsed_time < end_time); | ||
+ | } else { | ||
+ | | ||
+ | do { | ||
+ | | ||
+ | | ||
+ | } while (elapsed_time < end_time); | ||
+ | } | ||
+ | } | ||
+ | | ||
+ | } | ||
==== Breadth First Search==== | ==== Breadth First Search==== | ||
+ | {{ youtube> | ||
Algorithm for BFS | Algorithm for BFS | ||
+ | |||
+ | vector< | ||
+ | // | ||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | |||
+ | while (!queue.empty()) { | ||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | |||
+ | // | ||
+ | | ||
+ | |||
+ | if (findCurr == visited.end()) { | ||
+ | visited.push_back(currNode); | ||
+ | | ||
+ | vector< | ||
+ | | ||
+ | for (vector< | ||
+ | vector< | ||
+ | newPath.push_back(*it); | ||
+ | queue.push(newPath); | ||
+ | | ||
+ | if (*it == desiredNode) { //if desired found, return current path | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | //if no path, return empty vector | ||
+ | return vector< | ||
+ | } | ||
+ | |||
==== Depth First Search ==== | ==== Depth First Search ==== | ||
- | Algorithm for DFS | + | Algorithm for DFS. |
+ | \\ | ||
+ | There are two ways to traverse in DFS, an iterative approach and recursive approach. The following is a recursive approach because it is the most common. In the source code, there will be iterative as well. | ||
+ | |||
+ | |||
+ | vector< | ||
+ | // | ||
+ | | ||
+ | |||
+ | // | ||
+ | | ||
+ | |||
+ | //get adjacent nodes of starting node | ||
+ | | ||
+ | |||
+ | // | ||
+ | for ( vector< | ||
+ | const string& nextNode = *adjNodes; | ||
+ | |||
+ | //check if next node has been visited | ||
+ | if (!visited[nextNode]) { | ||
+ | // | ||
+ | vector< | ||
+ | |||
+ | //if subPath not empty, path to desired node is found | ||
+ | if (!subPath.empty()) { | ||
+ | // | ||
+ | | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | //if no path, return empty vector | ||
+ | return vector< | ||
+ | } | ||
==== Final Words ==== | ==== Final Words ==== | ||
Line 77: | Line 260: | ||
\\ | \\ | ||
\\ | \\ | ||
- | Speculating future work derived from this tutorial, includes | + | Speculating future work derived from this tutorial, includes |
\\ | \\ | ||
\\ | \\ |
bfs_and_dfs_implementation_v2.1699580361.txt.gz · Last modified: by fpalustre