Find all remaining vertices in Graph after marking shortest path for neighbours
Last Updated :
01 Mar, 2023
Given an undirected graph with V vertices in the form of an adjacency matrix, the weight of any two edges cannot be the same. For each vertex mark vertex that is connected to it via the edge of the lowest weight, the task is to return all the unmarked vertices. Return -1 if every vertex is marked.
Examples:
Input: V = 3, A[][] = {{0, 10, 50}, {10, 0, 30}, {50, 30, 0}}
Output: {2}
Explanation:
For vertex 0 -> 1 is marked,
for vertex 1 -> 0 is marked and
for vertex 2 -> 1 is marked
So 2 remained unmarked.
Input: V = 5, A[][] = {{0, 120, 150, 115, 200}, {120, 0, 30, 40, 170}, {150, 30, 0, 50, 160}, {115, 40, 50, 0, 155}, {200, 180, 160, 155, 0}}
Output: {0, 4}
Approach: To solve the problem follow the below idea:
The idea is to simply traverse each vertex and mark the vertex having connected to current vertex using the lowest weight edge, and find all the unmarked edges.
Follow the steps to solve this problem:
- Iterate the whole adjacency matrix using 2 loops i and j.
- For each vertex(row) find the lowest value in the adjacency matrix and the column number of that value would be the vertex having connected to the current vertex using the lowest weight edge,
- Mark this vertex for each vertex, using a boolean array.
- Print all the vertices not marked in the boolean array
Below is the implementation of this approach:
C++
#include <iostream>
#include <string>
#include <vector>
#include <limits>
class GFG
{
public :
static std::vector< bool > markedNodesCounter(std::vector<std::vector< int >> &A, int V)
{
std::vector< bool > marked(V);
for ( int i = 0; i < V; i++)
{
int minWeight = std::numeric_limits< int >::max();
int min = V + 1;
for ( int j = 0; j < V; j++)
{
if (i == j)
{
continue ;
}
if (A[i][j] < minWeight)
{
minWeight = A[i][j];
min = j;
}
}
marked[min] = true ;
}
return marked;
}
static void main(std::vector<std::string> &args)
{
int V = 3;
std::vector<std::vector< int >> A{{0, 10, 50}, {10, 0, 30}, {50, 30, 0}};
std::vector< bool > marked = GFG::markedNodesCounter(A, V);
bool flag = false ;
for ( int i = 0; i < V; i++)
{
if (!marked[i])
{
std::cout << std::to_string(i) + " " ;
flag = true ;
}
}
if (!flag)
{
std::cout << "-1" << std::endl;
}
else
{
std::cout << std::endl;
}
}
};
int main( int argc, char **argv){
std::vector<std::string> parameter(argv + 1, argv + argc);
GFG::main(parameter);
return 0;
};
|
Java
class GFG {
static boolean [] markedNodesCounter( int [][] A, int V)
{
boolean marked[] = new boolean [V];
for ( int i = 0 ; i < V; i++) {
int minWeight = Integer.MAX_VALUE;
int min = V + 1 ;
for ( int j = 0 ; j < V; j++) {
if (i == j)
continue ;
if (A[i][j] < minWeight) {
minWeight = A[i][j];
min = j;
}
}
marked[min] = true ;
}
return marked;
}
public static void main(String[] args)
{
int V = 3 ;
int A[][] = { { 0 , 10 , 50 },
{ 10 , 0 , 30 },
{ 50 , 30 , 0 } };
boolean marked[] = markedNodesCounter(A, V);
boolean flag = false ;
for ( int i = 0 ; i < V; i++) {
if (!marked[i]) {
System.out.print(i + " " );
flag = true ;
}
}
if (!flag)
System.out.println( "-1" );
else
System.out.println();
}
}
|
Python3
def markedNodesCounter(A, V):
marked = [ False ] * V
for i in range (V):
minWeight = float ( 'inf' )
min = V + 1
for j in range (V):
if i = = j:
continue
if A[i][j] < minWeight:
minWeight = A[i][j]
min = j
marked[ min ] = True
return marked
if __name__ = = '__main__' :
V = 3
A = [[ 0 , 10 , 50 ], [ 10 , 0 , 30 ], [ 50 , 30 , 0 ]]
marked = markedNodesCounter(A, V)
flag = False
for i in range (V):
if not marked[i]:
print (i, end = " " )
flag = True
if not flag:
print ( - 1 )
else :
print ()
|
C#
using System;
public class GFG
{
public static bool [] markedNodesCounter( int [,] A, int V)
{
bool [] marked = new bool [V];
for ( int i = 0; i < V; i++)
{
var minWeight = int .MaxValue;
var min = V + 1;
for ( int j = 0; j < V; j++)
{
if (i == j)
{
continue ;
}
if (A[i,j] < minWeight)
{
minWeight = A[i,j];
min = j;
}
}
marked[min] = true ;
}
return marked;
}
public static void Main(String[] args)
{
var V = 3;
int [,] A = {{0, 10, 50}, {10, 0, 30}, {50, 30, 0}};
bool [] marked = GFG.markedNodesCounter(A, V);
var flag = false ;
for ( int i = 0; i < V; i++)
{
if (!marked[i])
{
Console.Write(i.ToString() + " " );
flag = true ;
}
}
if (!flag)
{
Console.WriteLine( "-1" );
}
else
{
Console.WriteLine();
}
}
}
|
Javascript
function markedNodesCounter(A, V) {
let marked = new Array(V).fill( false );
for (let i = 0; i < V; i++) {
let minWeight = Number.MAX_SAFE_INTEGER;
let min = V + 1;
for (let j = 0; j < V; j++) {
if (i === j)
continue ;
if (A[i][j] < minWeight) {
minWeight = A[i][j];
min = j;
}
}
marked[min] = true ;
}
return marked;
}
let V = 3;
let A = [ [ 0, 10, 50 ],
[ 10, 0, 30 ],
[ 50, 30, 0 ] ];
let marked = markedNodesCounter(A, V);
let flag = false ;
for (let i = 0; i < V; i++) {
if (!marked[i]) {
console.log(i + " " );
flag = true ;
}
}
if (!flag)
console.log( "-1" );
else
console.log();
|
Time Complexity: O(V2), for using two nested loops from 0 to V.
Auxiliary Space: O(V), for creating an additional array marked of size V.
Share your thoughts in the comments
Please Login to comment...