Home > OS >  Algorithm to check whether all points between a list of two points linked to each other can be reach
Algorithm to check whether all points between a list of two points linked to each other can be reach

Time:03-10

Given several pairs of points, I am trying to figure out a way to determine if all points can be reached from each other.

For example, given these undirected paths between points [1,6]:

1 - 4
4 - 5
2 - 6
2 - 4
5 - 2

I can see that all points can be reached from each other.

However, for something like:

5 - 2
0 - 5
6 - 7

Points 6 and 7 cannot be reached from 0, 5, and 2.

I've tried a lot to play around with HashMaps and other ways to store the data but I can't seem to figure out a way to verify it all. Especially given points like 1 in the first example where there is only one instance of it.

I've been able to check whether there is a path between 2 given points:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class GondolaLiftsOne {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int n = in.nextInt(), start = in.nextInt(), destination = in.nextInt(), count = 0, entries = 0;
        HashMap<Integer, ArrayList<Integer>> paths = new HashMap<>();

        for (int i = 0; i < n; i  ) {
            int key = in.nextInt();
            if (!paths.containsKey(key)) paths.put(key, new ArrayList<>());

            entries  ;
            paths.get(key).add(in.nextInt());
        }

        while (destination != start) {
            if (count > entries*2) {
                System.out.println("No, but you can walk!");
                return;
            }
            destination = getPath(paths, destination);
            count  ;
        }
        System.out.println("YESSIREE");
    }

    public static int getPath (HashMap<Integer, ArrayList<Integer>> paths, int destination) {
        for (Map.Entry<Integer, ArrayList<Integer>> entry : paths.entrySet()) {
            for (int value : entry.getValue()) {
                if (value == destination) return entry.getKey();
            }
        }
        return -1;
    }
}

But otherwise, I have a hard time figuring out whether all points are connected. I can't seem to find any similar questions like this online.

CodePudding user response:

This is basically simple BFS for an undirected graph.

  1. Create an adjacency list
  2. If you have any vertex with in-degree 0, return False
  3. Pick up any node and start BFS from it by queueing its neighbours and maintaining a visited list

In the end, if all vertices are marked true, you know all points can be reached.

CodePudding user response:

Here is one algorithm you can use. No idea whether or not there's a better one.

Maintain a set of sets of points, where each of the sets contains points that are linked by a path. For each new edge that you encounter, find which sets each of the points on the edge belong to.

  1. If they're already both in the same set, there's nothing to do.
  2. If they're in different sets, then replace those two sets with a new set, that's the union of the two.
  3. If one is in a set, but the other isn't, then add the new point to the set.
  4. If they're both not already in sets, make a new set and add both points.

Once you've iterated through all the edges in this fashion, see how many sets you've got. If there's more than one, then your graph is not connected.

To see this in action, consider your first example.

  • Initially, our set of sets is empty. {}
  • Edge 1 4 is case 4, since neither point is in a set yet. So make a new set with both points in. {{1,4}}
  • Edge 4 5 is case 3, since 4 is already in a set. So add 5 to the set. {{1,4,5}}
  • Edge 2 6 is case 4, since neither point is in a set yet. Make a new set {{1,4,5},{2,6}}
  • Edge 2 4 is case 2, since 2 and 4 are in different sets. Delete both those sets and replace them with their union. {{1,4,5,2,6}}.
  • Edge 5 2 is case 1, since 2 and 5 are already in the same set.

At the end of the process, there's just one set, so the graph is connected.

  • Related