Perform and visualize Random Search
Theory
A random search algorithm works by randomly selecting points from a defined search space and evaluating them to find the best solution to a problem. It explores the space without following a specific pattern, Despite its simplicity, it can be effective in scenarios with large or complex search spaces where exhaustive search is impractical.
Algorithm For Random Search
- Function Input:
- The 'randomSearch' function takes nodes, links, a start node, and an end node as input.
- Initialization:
- Start with an empty path to keep track of the nodes visited during the search.
- Check Start Node:
- Verify if the start node has been visited previously..
- If it has, backtrack to the previous node and return false.
- Visit Start Node:
- Mark the start node as visited and add it to the path.
- Select Links:
- Identify all available links from the start node..
- Random Link Selection:
- Randomly choose one link from the available links.
- Check Target Node:
- Examine if the selected link leads to the end node
- If it does, return the path as the solution.
- If it doesn't, proceed to the next step.
- Recursive Call:
- Call the randomSearch function recursively on the target node.
- Backtracking:
- If no path is found from the current node (i.e., the link doesn't lead to the end node), backtrack to the previous node.
- Repeat:
- Repeat steps 3-9 until a path is found or all nodes have been explored.
- No Path Found:
- If no path is found after exploring all nodes, return false.