code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

01 Graph

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

class Graph {
  constructor() {
    this.edges = {}
  }

  addNode(node) {
    if (!this.edges[node]) {
      this.edges[node] = []
    }
  }

  addBidirectionalEdge(n1, n2) {
    this.addEdge(n1, n2)
    this.addEdge(n2, n1)
  }

  addEdge(start, end) {
    this.edges[start].push(end)
  }

  getNeighbours(start) {
    return this.edges[start]
  }
}

const gg = new Graph()
gg.addNode('Rajasthan')
gg.addNode('Gujrat')
gg.addNode('Uttar Pradesh')
gg.addNode('Madhya Pradesh')
gg.addNode('Maharashtra')

gg.addBidirectionalEdge('Rajasthan', 'Gujrat')
gg.addBidirectionalEdge('Rajasthan', 'Uttar Pradesh')
gg.addBidirectionalEdge('Gujrat', 'Uttar Pradesh')
gg.addBidirectionalEdge('Gujrat', 'Maharashtra')
gg.addBidirectionalEdge('Gujrat', 'Madhya Pradesh')
gg.addBidirectionalEdge('Uttar Pradesh', 'Madhya Pradesh')
gg.addBidirectionalEdge('Maharashtra', 'Madhya Pradesh')

console.log('Neighbour(Rajasthan) -> ' + gg.getNeighbours('Rajasthan'))
console.log('Neighbour(Gujrat) -> ' + gg.getNeighbours('Gujrat'))
console.log('Neighbour(Madhya Pradesh) -> ' + gg.getNeighbours('Madhya Pradesh'))

function breadthFirstSearch() {
  let cityGraph = ['Rajasthan']

  let visited = {}

  while (cityGraph.length > 0) {
    let node = cityGraph.shift()
    console.log(node)
    visited[node] = true
    for (let neighbour of gg.getNeighbours(node)) {
      if (!visited[neighbour]) {
        cityGraph.push(neighbour)
        visited[neighbour] = true
      }
    }
  }
}

function depthFirstSearch() {
  let cityGraph = ['Rajasthan']

  let visited = {}

  while (cityGraph.length > 0) {
    let node = cityGraph.pop()
    console.log(node)
    visited[node] = true
    for (let neighbour of gg.getNeighbours(node)) {
      if (!visited[neighbour]) {
        cityGraph.push(neighbour)
        visited[neighbour] = true
      }
    }
  }
}

console.log('')
console.log('Breadth first ->')
breadthFirstSearch()
console.log('')
console.log('Depth first ->')
depthFirstSearch()

Created 2019-12-03T23:47:11+05:18, updated 2019-12-04T02:03:28+05:18 · History · Edit