Skip to content
Snippets Groups Projects
Select Git revision
  • 06d8916b365d6567dca21d36a0458386ca636e87
  • main default protected
  • solution
3 results

solution.js

Blame
  • solution.js 1.62 KiB
    /* eslint-disable no-magic-numbers */
    
    import { DirectedGraph, PuzzleGraph } from './graphs.js';
    // eslint-disable-next-line no-unused-vars
    import { dfs, bfs, dijkstras, bestFirst, recursiveDFS } from './search.js';
    
    import firstExampleImage from './images/firstExample.svg';
    import secondExampleImage from './images/secondExample.svg';
    
    const firstExample = new DirectedGraph();
    firstExample.addIncidence('b', 9, 'e');
    firstExample.addIncidence('f', 8, 'c');
    firstExample.addIncidence('a', 7, 'c');
    firstExample.addIncidence('d', 6, 'b');
    firstExample.addIncidence('c', 5, 'e');
    firstExample.addIncidence('b', 4, 'a');
    firstExample.addIncidence('e', 3, 'd');
    firstExample.addIncidence('e', 2, 'f');
    firstExample.addIncidence('c', 1, 'b');
    firstExample.addIncidence('f', 0, 'd');
    
    const secondExample = new PuzzleGraph();
    
    function formatSolution(solution) {
      if (solution === undefined) {
        return '[no solution]';
      }
      return solution.join(' → ');
    }
    
    export function Solution(props) {
      const search = bestFirst;
      const firstSolution = search(firstExample, 'a', 'd');
      const secondSolution = search(secondExample, '123', '321');
      return (
        <>
          <div>
            <figure>
              <img src={firstExampleImage} alt="A weighted directed graph." />
            </figure>
            <label>
              First example: <output>{formatSolution(firstSolution)}</output>
            </label>
          </div>
          <div>
            <figure>
              <img src={secondExampleImage} alt="An undirected puzzle graph." />
            </figure>
            <label>
              Second example: <output>{formatSolution(secondSolution)}</output>
            </label>
          </div>
        </>
      );
    }