#count number of connected graphs

5 messages · Page 1 of 1 (latest)

buoyant kite
#

I'm trying to count the number of connected graphs but it's not working in my code. Does anyone know what's wrong with my code?

    public int minNumAssistanceCars() {
        // Add your code here to compute and return the minimum number of assistance cars required for this map
        ArrayList<Vertex> roads_edges = new ArrayList<Vertex>();
        int counter = 0;
        Set<Vertex> visited = new HashSet<Vertex>();

        for (Vertex vertex : places) {
            if (!visited.contains(vertex.getIndex())) {
            counter++;
            DFS(vertex, roads_edges);
            }
        }
        return counter;
        }
        

        private void DFS(Vertex startVertex, ArrayList<Vertex> pathedges) {
        Set<Vertex> visited = new HashSet<Vertex>();
        visited.add(startVertex.getIndex());
        ArrayList<Edge> adjacent_nodes = startVertex.getIncidentRoads();

        for (Edge edge : adjacent_nodes) {
            Vertex added_nodes = edge.getFirstVertex().getIndex() != startVertex.getIndex()
                                ? edge.getFirstVertex()
                                : edge.getSecondVertex();
            if (!visited.contains(added_nodes.getIndex())) {
                pathedges.add(added_nodes);
            }
        }
        for (Vertex vertex : pathedges) {
            if (!visited.contains(vertex.getIndex())) {
            DFS(vertex, pathedges);
            }
        }
        

        return 0;

    }
ebon onyxBOT
#

This post has been reserved for your question.

Hey @buoyant kite! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

ebon onyxBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.

ebon onyxBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.