Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
public static void main (String [] args){

        Scanner sc= new Scanner (System.in);
        System.out.println("Enter no. of Islands");
        int n= sc.nextInt();
         Graph g = new Graph (n);
        System.out.println("Enter no. of one-way bridges");
        int m= sc.nextInt();
        System.out.println("Enter no. of island you want to be intially on");
        int r= sc.nextInt();
       try{ for (int i=0; i<m;i++){ 
            System.out.println("This one-way bridge connects between");
            int u = sc.nextInt();
            int v = sc.nextInt();
            if(u == v || u>n || v>n){ throw new Bounds("");}
            else{ g.addEgde(u-1, v-1);}

        }
                g.topoSort();}
        catch(IndexOutOfBoundsException e){
            System.out.println("Please enter a valid input!");
        }
       catch(Bounds e){
       System.out.println("Please enter a valid input!");
       }


    }
    public static class Bounds extends Exception{

    public Bounds (String message){
        super(message);
    }}



    static class Graph {
        int V;
        LinkedList<Integer>[] adjList;

        Graph(int V) {
            this.V = V;
            adjList = new LinkedList[V];
            for (int i = 0; i < V; i++) {
                adjList[i] = new LinkedList<>();
            }
        }

        public void addEgde(int u, int v) {
            adjList[u].addFirst(v);
        }

        public void topoSort() {
            boolean[] visited = new boolean[V];
            stack stack = new stack();
            for (int i = 0; i < V; i++) {
                if (!visited[i]) {
                    topoSortRE(i, visited, stack);
                }
            }
            System.out.println("Topological Sort: ");
            int size = stack.size();
            for (int i = 0; i <size ; i++) {
                System.out.print(stack.pop()+ 1 + " ");
            }
        }

        public void topoSortRE(int s, boolean[] visited, stack stack) {
            visited[s] = true;
            for (int i = 0; i < adjList[s].size(); i++) {
                int vertex = adjList[s].get(i);
                if (!visited[vertex])
                    topoSortRE(vertex, visited, stack);
            }
            stack.push(s);
        }}}


What I have tried:

The following code is an attempt to solve the following problem: There are many islands that are connected by one-way bridges, that is, if a bridge connects islands a and b, then you can only use the bridge to go from a to b but you cannot travel back by using the same. If you are on island a, then you select (uniformly and randomly) one of the islands that are directly reachable from a through the one-way bridge and move to that island. You are stuck on an island if you cannot move any further. It is guaranteed that if there is a directed path from one island to the second there is no path that leads from the second back to the first. In other words the formed graph is a Directed Acyclic Graph. Find the island that you are most likely to get stuck on; that is the island that you can possibly reach with the maximum number of paths from all other islands.

Input format: First line: Three integers n (the number of islands), m (the number of one-way bridges), and r (the index of the island you are initially on) Next m lines: Two integers ui and vi representing a one-way bridge from island ui to vi. Output format: Print the index of the island that you are most likely to get stuck on. If there are multiple islands, then print them in the increasing order of indices (space separated values in a single line). Sample input

5, 7, 1
(1, 2)
(1, 3)
(1, 4)
(1, 5)
(2, 4)
(2, 5)
(3, 4)
Sample output

4
I wrote the code to topologically sort the graph but I am having issues with how to make the input r the intial island and also how to make the output be the most probable island to be stuck on. I know that the island I'm most likely to be stuck on is the island that has the most indegrees and no outdegrees but don't know how to implement that.
Posted
Comments
karma2447 13-Jul-20 1:01am    
Your idea is correct,
1. In case there is no memory bound, and you already know the number of the island, so you can create array of that length. arrayThatStoresOutDegree[#islands]
2. When bounded by space, add island to list of vertex, meaning your Graph will look and if present just increment outbounds connections.
Best way to do is, Map<#island, ouboundList>.

Also, to answer your question, use add method, instead
public void addEgde(int u, int v) {
adjList[u].add(v);
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900