Skip to main content

Find all mother vertex of a directed graph in java

Java program for Find all mother vertex of a directed graph. Here problem description and explanation.

/*
    Java program for 
    Find all Mother Vertex in a Graph
*/
class AjlistNode
{
    // Vertices node key
    public int id;
    public AjlistNode next;
    public AjlistNode(int id)
    {
        // Set value of node key
        this.id = id;
        this.next = null;
    }
}
class Vertices
{
    public int data;
    public AjlistNode next;
    public AjlistNode last;
    public Vertices(int data)
    {
        this.data = data;
        this.next = null;
        this.last = null;
    }
}
public class Graph
{
    // Number of Vertices
    public int size;
    public Vertices[] node;
    public Graph(int size)
    {
        // Set value
        this.size = size;
        this.node = new Vertices[size];
        this.setData();
    }
    // Set initial node value
    public void setData()
    {
        if (size <= 0)
        {
            System.out.println("\nEmpty Graph");
        }
        else
        {
            for (int index = 0; index < size; index++)
            {
                // Set initial node value
                node[index] = new Vertices(index);
            }
        }
    }
    //  Handling the request of adding new edge
    public void addEdge(int start, int last)
    {
        if (start >= 0 && start < size && last >= 0 && last < size)
        {
            // Safe connection
            AjlistNode edge = new AjlistNode(last);
            if (node[start].next == null)
            {
                node[start].next = edge;
            }
            else
            {
                // Add edge at the end
                node[start].last.next = edge;
            }
            // Get last edge 
            node[start].last = edge;
        }
        else
        {
            // When invalid nodes
            System.out.println("\nHere Something Wrong");
        }
    }
    public void printGraph()
    {
        if (size > 0)
        {
            // Print graph ajlist Node value
            for (int index = 0; index < size; ++index)
            {
                System.out.print("\nAdjacency list of vertex " + index + " :");
                AjlistNode temp = node[index].next;
                while (temp != null)
                {
                    // Display graph node 
                    System.out.print("  " + node[temp.id].data);
                    // Visit to next edge
                    temp = temp.next;
                }
            }
        }
    }
    // Check Path of given vertex
    public void dfs(int point, boolean[] path)
    {
        if (path[point])
        {
            // When alredy visit
            return;
        }
        else
        {
            // Collect the current node into the path
            path[point] = true;
            AjlistNode temp = node[point].next;
            while (temp != null)
            {
                // Visit to edge node
                dfs(temp.id, path);
                // Visit to next edge
                temp = temp.next;
            }
        }
    }
    public void printMotherVertices()
    {
        if (this.size <= 0)
        {
            // No nodes
            return;
        }
        System.out.print("\nMother Vertex :");
        boolean[] path = new boolean[this.size];
        int n = 0;
        boolean status = true;
      	int count = 0;
        while (n < this.size)
        {
            // Check path of node n
            this.dfs(n, path);
            // When current node are visit other nodes
            // Then that is a Mother Vertex
            for (int index = 0; index < this.size; index++)
            {
                if (path[index] == false)
                {
                    // When path not contain current node
                    status = false;
                }
                // Reset element value
                path[index] = false;
            }
            if (status)
            {
                // Display result node
                System.out.print("  " + n);
              	count ++;
            }
            // Reset status
            status = true;
            // Visit next node
            n++;
        }
      	if(count == 0)
        {
             System.out.println("Node");
        }
    }
    public static void main(String[] args)
    {
        // 7 implies the number of nodes in graph
        Graph g = new Graph(7);
        // Connect node with an edge
        g.addEdge(1, 0);
        g.addEdge(1, 4);
        g.addEdge(2, 5);
        g.addEdge(3, 1);
        g.addEdge(3, 2);
        g.addEdge(3, 6);
        g.addEdge(4, 3);
        g.addEdge(5, 0);
        // Print graph element
        g.printGraph();
        // Test
        g.printMotherVertices();
    }
}

Output

Adjacency list of vertex 0 :
Adjacency list of vertex 1 :  0  4
Adjacency list of vertex 2 :  5
Adjacency list of vertex 3 :  1  2  6
Adjacency list of vertex 4 :  3
Adjacency list of vertex 5 :  0
Adjacency list of vertex 6 :
Mother Vertex :  1  3  4




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment