Open in App
Not now

# Maximum value of a Node that can be reached using given connections

• Last Updated : 14 Mar, 2023

Given a set of 109 nodes. There are N bidirectional connections, and ith connection connects the Aithâ€‹ and Bithâ€‹ nodes. Starting from the first node, find the maximum value of a node that can be reached using given connections.

Examples:

Input:  N = 4
1  4
4  6
4  10
7  3
Output: 10
Explanation: Move from 1st node to 4th node using connection 1, then move to 10th node from 4th node using connection 3.

Input:  N = 3
5   6
6   70
70  800
Output:  1
Explanation: You are initially on node 1 and there is no connection from the 1st node so you can’t reach anywhere.

Approach: The problem can be solved using DFS based on the following idea.

The idea/intuition is to start from 1st node and check the nodes we can reach from it directly or through any connected path. Build a graph of nodes where connections connect any two floors, then run dfs and update maximum at each step.

Follow the steps mentioned below to implement the idea:

• Make an unordered map for marking connections between the nodes.
• Make a set, which will tell whether the node is visited before in dfs traversal.
• Start dfs traversal from node 1, mark the current node as visited in the set and update maximum with the current node.
• Traverse in all child nodes of the current node, if the child node is not visited call dfs for the child node.
• At last, when all connected nodes are traversed, we have the maximum node reachable in res.

Below is the implementation of the above approach.

## C++

```// C++ code to implement the approach

#include <bits/stdc++.h>
using namespace std;

// Utility function to add edge
// between node u and v
{
}

// Global variable to store max
// reachable node
int res = 1;

// DFS function to traverse the graph
void dfs(map<int, vector<int> >& adj, set<int>& vis,
int root)
{
// Mark the root node as visited
vis.insert(root);

// Update res with maximum of res
// and current node
res = max(res, root);

// Traverse in child nodes
for (int child : adj[root]) {

// If it's not visited
if (vis.find(child) == vis.end())
}
}

// Driver Code
int main()
{
int n = 4;

// Adjacency list for storing graph

// Taking n connections input

// Set to mark visited node
set<int> vis;

// dfs call with initial node 1
cout << res;

return 0;
}```

## Java

```// Java code to implement the approach

import java.io.*;
import java.util.*;
import java.util.HashMap;

public class GFG {

// Utility function to add edge
// between node u and v
{
else{
}
else{
}
}

// Global variable to store max
// reachable node
static Integer res = 1;

// DFS function to traverse the graph
static void dfs(HashMap<Integer, ArrayList<Integer>> adj, Set<Integer> vis,
Integer root)
{
// Mark the root node as visited

// Update res with maximum of res
// and current node
res = Math.max(res, root);

// Traverse in child nodes
{
{
// If it's not visited
}
}
}
}

// Driver Code
public static void main(String[] args)
{
int n = 4;

// Adjacency list for storing graph
HashMap<Integer, ArrayList<Integer>> adj = new HashMap<>();

// Taking n connections input

// Set to mark visited node
Set<Integer> vis = new HashSet<Integer>();

// dfs call with initial node 1
System.out.println(res);

}

}

// This code is contributed by Pushpesh Raj.```

## Python3

```# python code implementation
from collections import defaultdict

# Utility function to add edge
# between node u and v

# Global variable to store max
# reachable node
res = 1

# DFS function to traverse the graph

# Mark the root node as visited

# Update res with maximum of res
# and current node
global res
res = max(res, root)

# Traverse in child nodes
# If it's not visited
if child not in vis:

# Driver Code
if __name__ == "__main__":
n = 4

# Adjacency list for storing graph

# Taking n connections input

# Set to mark visited node
vis = set()

# dfs call with initial node 1
print(res)

# This code is contributed by ksam24000
```

## C#

```using System;
using System.Collections.Generic;

class MainClass {
// Utility function to add edge
// between node u and v
int u, int v)
{
}
}
}

// Global variable to store max
// reachable node
static int res = 1;

// DFS function to traverse the graph
static void dfs(Dictionary<int, List<int> > adj,
HashSet<int> vis, int root)
{
// Mark the root node as visited

// Update res with maximum of res
// and current node
res = Math.Max(res, root);

// Traverse in child nodes
{
// If it's not visited
if (!vis.Contains(child)) {
}
}
}
}

// Driver Code
public static void Main(string[] args)
{
int n = 4;

// Adjacency list for storing graph
= new Dictionary<int, List<int> >();

// Taking n connections input

// Set to mark visited node
HashSet<int> vis = new HashSet<int>();

// dfs call with initial node 1
Console.WriteLine(res);
}
}```

## Javascript

```let adj = new Map();
let vis = new Set();
let res = 1;

}
}
}

res = Math.max(res, root);
for (let child of adj.get(root)) {
if (!vis.has(child)) {
}
}
}

console.log(res);
```
Output

`10`

Output:

`10`

Time complexity: O(V + E), where V is the number of vertices and E is the number of edges in the graph.
Auxiliary Space: O(V), since an extra visited array of size V is required.

Approach (Using BFS):

1.  Make an Adjanceny list to store the graph
2.  Create a set, which will tell whether the node is visited before in dfs traversal.
3.  Start BFS traversal from node 1, mark the current node as visited in the set and update maximum with the current node. Create a        Queue
4.  Traverse in all child(adjacent) nodes of the current node, if the child node is not visited add it into the queue
5.   At last, when all connected nodes are traversed, we have the maximum node reachable in res.

Below is the implementation of the above approach.

## C++

```#include <iostream>
#include <unordered_map>
#include <vector>
#include <queue>
#include <unordered_set>

using namespace std;

// Utility function to add edge between node u and v
else {
}
else {
}
}

int res = 1;

// creating an unordered_set to store visited nodes
unordered_set<int> visited;
visited.insert(res);
// Adding 1 because we are starting from there
// Update the res with maximum of res and current node
queue<int> q;
q.push(1);
while (!q.empty()) {
int curr = q.front();
q.pop();
visited.insert(curr);
// Update res with maximum of res and current node
res = max(res, curr);
if (visited.find(x) == visited.end())
q.push(x);
}
return res;
}

int main() {
int n = 4;

// Adjacency list for storing graph

// Taking n connections input

// bfs call
cout << res << endl;
return 0;
}
```

## Java

```import java.util.*;
class Main {
// Utility function to add edge
// between node u and v
static void
Integer u, Integer v)
{
else {
}
else {
}
}
static int res = 1;
public static int max_node_value(
{
// creating a hashset to store visited nodes
HashSet<Integer> visited = new HashSet<>();
// Adding 1 because we are
// starting from there
// Update the res with maximum of res
// and current node
while (!queue.isEmpty()) {

int curr = queue.poll();
// Update res with maximum of res
// and current node
res = Math.max(res, curr);
if (!visited.contains(x))
}
return res;
}

public static void main(String[] args)
{
int n = 4;

// Adjacency list for storing graph
= new HashMap<>();

// Taking n connections input

// bfs call
System.out.println(res);
}
}```

## Python3

```import collections

# Utility function to add edge between node u and v
else:
else:

res = 1

global res
# creating an unordered_set to store visited nodes
visited = set()
# Adding 1 because we are starting from there
# Update the res with maximum of res and current node
q = collections.deque([1])
while q:
curr = q.popleft()
# Update res with maximum of res and current node
res = max(res, curr)
if x not in visited:
q.append(x)
return res

# Adjacency list for storing graph

# Taking n connections input

# bfs call
print(res)

# This code is contributed by Akash Jha```

## C#

```using System;
using System.Collections.Generic;

class MainClass {
// Utility function to add edge
// between node u and v
int u, int v)
{
else {
}
else {
}
}
static int res = 1;
public static int
{
// creating a hashset to store visited nodes
HashSet<int> visited = new HashSet<int>();
// Adding 1 because we are
// starting from there
// Update the res with maximum of res
// and current node
queue.Enqueue(1);
while (queue.Count > 0) {

int curr = queue.Dequeue();
// Update res with maximum of res
// and current node
res = Math.Max(res, curr);
foreach(int x in
queue.Enqueue(x);
}
return res;
}

public static void Main(string[] args)
{
int n = 4;

// Adjacency list for storing graph
= new Dictionary<int, List<int> >();

// Taking n connections input

// bfs call
Console.WriteLine(res);
}
}
```

## Javascript

```// Utility function to add edge between node u and v
else {
}
else {
}
}

let res = 1;

// creating an unordered_set to store visited nodes
let visited = new Set();

// Adding 1 because we are starting from there
// Update the res with maximum of res and current node
let q = [];
q.push(1);
while (q.length !== 0) {
let curr = q.shift();

// Update res with maximum of res and current node
res = Math.max(res, curr);
if (!visited.has(x))
q.push(x);
}
return res;
}

// Adjacency list for storing graph

// Taking n connections input

// bfs call
console.log(res);

// This code is contributed by Prajwal Kandekar```
Output

```10
```

Time complexity: O(V + E), where V is the number of vertices and E is the number of edges in the graph.
Auxiliary Space: O(V) , v is the number of vertices

Related Articles:

My Personal Notes arrow_drop_up
Related Articles