R

E

A

C

T

O

xamples 

epeat

ode

pproach

ptimize

est

Graph Solver

The Question

Write a function that can find out if a 'graph' has a solution from a given start point to an end point

1. Given an array like [{start: 'a', end: 'b'}, {start: 'b', end: 'c'}] and start point 'a', and end point 'c'.

2. Loop through the objects in the array to determine if you can arrive at 'c' from point 'a'.

3. The array may not be sorted, and may be of arbitrary length.

4. Return true if the 'graph' is solvable, false if not.

Approach

array = [

{start:'b', end:'a'},

{start:'f', end:'g'},

{start:'a', end:'c'},

{start:'c', end:'f'},

{start:'d', end:'b'}

]

start='a', end='f'

  1. Assign a variable to keep track of current point. Initial value is the given start point 'a'.
  2. Loop through the array until array[n].start === 'a', in this case array[2].start === a.
  3. Assign array[2].end to current_point
  4. Loop through the array again to find the next connection point (in this case 'c' to 'b').

Edge Cases

The graph can loop in on itself, ex: a -> b -> a. So you need to keep track of points you have visited and not reset current_point to arcs[n].end if arcs[n].end is a previously visited point.

There is an edge case not covered by the iterative solution, where in the case of an arcs array [{a,b},{b,c},{b,d}], and inputs start=a, end=d, the function will return false because it sets current_point to b in arcs[0], then b to c in arcs[1] and then there is no point c->d so it returns false, even though b->d is one of the points. It does this because b->c is before b->d. Basically, if someone brings up this edge case, tell them they do Not have to account for it, but if they do, great!

An iterative solution

function solve_graph(start, end, arcs) {

  var current_point = start; // Assign current_point initial value to start value

  var prev_points = []; // Initialize an array to keep track of 
                        // previously visited points (in case 
                        // the an arc loops back to a previously visited point)

  if(start === end) return true; // Check that start and end arent the 
                                 // same, if they are you are already finished

  for(var n = 0, len = arcs.length; n < len; n++) {
    
    // This checks if both the current point is the start of the current arc, and that
    // the current arc's end point has not been visited before
    if(arcs[n].start === current_point && prev_points.indexOf(arcs[n].end) === -1) {

      // if the above condition is true, add the current point to the list of past points
      prev_points.push(current_point);

      current_point = arcs[n].end; // assign the current point to be the current arc's end
      
      n = -1; // start looping from the beginning of the array
    }
    
    // if your current point is equal to your end point, you have reached your destination --> return true
    if(current_point === end) return true;
  }

  // if you ever get to the end of the array, that means the chain was broken
  // and the graph is unsolvable --> return false
  return false;
}

Other possible solutions: recursive

function solve_graph(start, end, arcs, seen) {

  var res = false;

  seen = seen || [];

  if (start === end) {return true} // Base case

  arcs.forEach(function(arc){
    // Similar condition to iterative
    if (arc.start === start && seen.indexOf(arc) < 0) {
      seen.push(arc); // add to seen (previously visited point)

      // recursive call with start point as current arc's end point
      if (solve_graph(arc.end,end,arcs,seen)) {
        return res = true
      }
    }
  });

  return res;
}

Complexity  - O(n^2)

If N is the size of the array, then the worst-case is that you have to loop over the array N times.

 

If all the arcs are connected, and the given end point is at the end of the path, or not present at all you loop over the array N times, going on average through half the array (disregard the constant for big-O). NxN

The End

Copy of Graph Solver

By Joanne Yae

Copy of Graph Solver

Technical interview problem on binary search

  • 241