#time complexity

3 messages · Page 1 of 1 (latest)

hearty jay
#

@analog drift I have created a new algorithm and tried to implement DFS search to it, do you know if this algorithm will meet the time complexity of o(v + e)?

    public int minNumAssistanceCars() {
        // Add your code here to compute and return the minimum number of assistance cars required for this map
                int counter = 0;
        Set<Integer> visited = new HashSet<Integer>();
        for (Vertex vertex : places) {
            if (!visited.contains(vertex.getIndex())) {
                counter++;
                DFS(vertex, visited);
            }
        }
        return counter;
    }

    private void DFS(Vertex startVertex, Set<Integer> visited) {
        visited.add(startVertex.getIndex());
        ArrayList<Edge> adjacentNodes = startVertex.getIncidentRoads();
        for (Edge edge : adjacentNodes) {
            Vertex adjacentVertex = edge.getFirstVertex().getIndex() != startVertex.getIndex()
                    ? edge.getFirstVertex()
                    : edge.getSecondVertex();
            if (!visited.contains(adjacentVertex.getIndex())) {
                DFS(adjacentVertex, visited);
            }
        }
    }
uneven flameBOT
#

This post has been reserved for your question.

Hey @hearty jay! 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.