Question: code javascript: Check whether the given graph contains a path going through all vertices, one by one, in increasing order of their numbers. Task description
code javascript: Check whether the given graph contains a path going through all vertices, one by one, in increasing order of their numbers.
Task description
You are given an undirected graph consisting of N vertices, numbered from to N and M edges.
The graph is described by two arrays, A and B both of length M A pair AK BK for K from to M describes an edge between vertex AK and vertex BK
Your task is to check whether the given graph contains a path from vertex to vertex N going through all of the vertices, one by one, in increasing order of their numbers. All connections on the path should be direct.
Write a function:
function solutionN A B;
that, given an integer N and two arrays A and B of M integers each, returns true if there exists a path from vertex to N going through all vertices, one by one, in increasing order, or false otherwise.
Examples:
Given N A and B the function should return true. There is a path using edges and
Given N A and B the function should return false. There is no path as there is no direct connection from vertex to vertex
Given N A and B the function should return false. There is no direct connection from vertex to vertex
Given N A and B the function should return true. There is a path using edges and
Write an efficient algorithm for the following assumptions:
N is an integer within the range ;
M is an integer within the range ;
all elements of arrays A and B are integers within the range N;
there are no selfloops edges with AK BK in the graph;
there are no multiple edges between the same vertices.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
