Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started