Check if alternate path exists from U to V with smaller individual weight in a given Graph

Given a directed weighted graph with N vertices and M edges and an edge (U, V). The task is to find whether there is an alternate path present from U to V with individual weight of edges in alternate path less than the weight of direct path. If present print Yes else print No.

Examples

For the given directed graph:

Input: N = 7, M = 10, U = 3, V = 6.
Output: No
Explanation:
For the given edge {3, 6}, weight = 16. There is no alternate path to reach 6 from 3. Hence the answer is No.

Input: N = 7, M = 10, U = 1, V = 6.
Output: Yes
Explanation:
=> For the given edge {1, 6}, weight = 5.
=> Alternate path to reach 6 from 1 = {1, 5, 6} with individual weights {12, 2} which is more than 5. Hence this path cannot be considered.
=> Alternate path to reach 6 from 1 = {1, 2, 4, 5, 6} with individual weights {5, 1, 1, 2} which is not less than 5. Hence this path cannot be considered.
=> Alternate path to reach 6 from 1 = {1, 4, 5, 6} with individual weights {3, 1, 2} which is less than 5. Hence this path can be considered.
=> Hence the answer is Yes



Approach:

  • Traverse the given Directed Graph with starting vertex U using depth-first search (DFS) Traversal.
  • During DFS Traversal if weight of any edge is greater than the directed edge, then that path is not included.
  • If we reach the vertex V with weight of each edge in traverse path less than directed edge, then there exists an alternate path.
  • Else there is no path between vertex U and vertex V other than direct path.

Below is the implementation of the above approach:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for above approach
  
import java.util.*;
  
// To ignore the unchecked warning
@SuppressWarnings("unchecked")
  
// GfG class
public class GfG {
  
    // Array to mark already
    // visited vertices
    static private boolean visited[];
  
    // Adjacency list representation
    // of the graph
    static private ArrayList<Edge> graph[];
  
    // GfG class constructor
    public GfG(int size)
    {
        visited = new boolean[size];
        graph = new ArrayList[size];
    }
  
    // Edge class
    static class Edge {
        int u;
        int v;
        int w;
  
        // Edge constructor to
        // initialize edge (u, v) with
        // weight w
        Edge(int u, int v, int w)
        {
            this.u = u;
            this.v = v;
            this.w = w;
        }
    }
  
    // Helper method to
    // initialize graph
    static private void helperInitialize(int size)
    {
        for (int i = 0; i < size; i++) {
            graph[i] = new ArrayList<Edge>();
        }
    }
  
    // Depth First Search to traverse
    // all vertices with weight less
    // than weight of the dfs root
    static private void dfs(int S, int W)
    {
  
        // Marking the vertex visited
        visited[S] = true;
  
        // Traversing adjacent vertex
        for (Edge uv : graph[S]) {
            int ver = uv.v;
            int w = uv.w;
            if (!visited[ver] && w < W)
                dfs(ver, W);
        }
    }
  
    // Driver function
    public static void main(String[] args)
    {
  
        // Number of vertices
        int N = 7;
  
        // Number of edges
        int M = 10;
  
        // Edge to be checked
        int U_V[] = { 3, 6 };
  
        // Creating GfG object
        GfG obj = new GfG(8);
  
        // Initializing graph
        helperInitialize(8);
  
        // Creating edges
        Edge e0 = new Edge(1, 2, 5);
        Edge e1 = new Edge(1, 4, 3);
        Edge e2 = new Edge(1, 5, 12);
        Edge e3 = new Edge(1, 6, 5);
        Edge e4 = new Edge(4, 5, 1);
        Edge e5 = new Edge(5, 6, 2);
        Edge e6 = new Edge(5, 3, 1);
        Edge e7 = new Edge(3, 6, 16);
        Edge e8 = new Edge(4, 7, 1);
        Edge e9 = new Edge(2, 4, 1);
  
        // Adding edges to the graph
        graph[1].add(e0);
        graph[1].add(e1);
        graph[1].add(e2);
        graph[1].add(e3);
        graph[4].add(e4);
        graph[5].add(e5);
        graph[5].add(e6);
        graph[3].add(e7);
        graph[4].add(e8);
        graph[2].add(e9);
  
        // DFS traversal from
        // vertex U
        dfs(U_V[0], 16);
  
        // If there is alternate
        // path then print YES,
        // else NO
        if (visited[U_V[1]]) {
            System.out.print("No");
        }
        else {
            System.out.print("Yes");
        }
    }
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for above approach
using System;
using System.Collections.Generic;
  
// GfG class
class GfG
{
  
    // Array to mark already
    // visited vertices
    static private bool []visited;
  
    // Adjacency list representation
    // of the graph
    static private List<Edge> []graph;
  
    // GfG class constructor
    public GfG(int size)
    {
        visited = new bool[size];
        graph = new List<Edge>[size];
    }
  
    // Edge class
    class Edge
    {
        public int u;
        public int v;
        public int w;
  
        // Edge constructor to
        // initialize edge (u, v) with
        // weight w
        public Edge(int u, int v, int w)
        {
            this.u = u;
            this.v = v;
            this.w = w;
        }
    }
  
    // Helper method to
    // initialize graph
    static private void helperInitialize(int size)
    {
        for (int i = 0; i < size; i++) 
        {
            graph[i] = new List<Edge>();
        }
    }
  
    // Depth First Search to traverse
    // all vertices with weight less
    // than weight of the dfs root
    static private void dfs(int S, int W)
    {
  
        // Marking the vertex visited
        visited[S] = true;
  
        // Traversing adjacent vertex
        foreach (Edge uv in graph[S])
        {
            int ver = uv.v;
            int w = uv.w;
            if (!visited[ver] && w < W)
                dfs(ver, W);
        }
    }
  
    // Driver function
    public static void Main(String[] args)
    {
  
        // Edge to be checked
        int []U_V = { 3, 6 };
  
        // Creating GfG object
        GfG obj = new GfG(8);
  
        // Initializing graph
        helperInitialize(8);
  
        // Creating edges
        Edge e0 = new Edge(1, 2, 5);
        Edge e1 = new Edge(1, 4, 3);
        Edge e2 = new Edge(1, 5, 12);
        Edge e3 = new Edge(1, 6, 5);
        Edge e4 = new Edge(4, 5, 1);
        Edge e5 = new Edge(5, 6, 2);
        Edge e6 = new Edge(5, 3, 1);
        Edge e7 = new Edge(3, 6, 16);
        Edge e8 = new Edge(4, 7, 1);
        Edge e9 = new Edge(2, 4, 1);
  
        // Adding edges to the graph
        graph[1].Add(e0);
        graph[1].Add(e1);
        graph[1].Add(e2);
        graph[1].Add(e3);
        graph[4].Add(e4);
        graph[5].Add(e5);
        graph[5].Add(e6);
        graph[3].Add(e7);
        graph[4].Add(e8);
        graph[2].Add(e9);
  
        // DFS traversal from
        // vertex U
        dfs(U_V[0], 16);
  
        // If there is alternate
        // path then print YES,
        // else NO
        if (visited[U_V[1]])
        {
            Console.Write("No");
        }
        else
        {
            Console.Write("Yes");
        }
    }
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output:

Yes

Time Complexity: O(N + M), where N = number of vertices & M = munber of edges.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : 29AjayKumar