Search in sources :

Example 21 with Queue

use of com.google.cloud.tasks.v2.Queue in project algorithms-sedgewick-wayne by reneargento.

the class Exercise26_CriticalEdges method findCriticalEdges.

/**
 * An edge e is critical if and only if it is a bridge in the subgraph containing all edges with weights
 * less than or equal to the weight of edge e.
 *
 * Proof:
 * 1st part: If an edge e is critical then it is a bridge in the subgraph containing all edges with weights
 * less than or equal to the weight of edge e.
 * Consider by contradiction that edge e is not a bridge in such subgraph. If it is not a bridge, then there is another
 * edge f that connects the same components as e in the subgraph and it has weight less than or equal to e.
 * In this case, edge e could be replaced by edge f in an MST and the MST weight would not increase.
 * However, since e is critical and cannot be replaced by an edge with weight less than or equal to it,
 * it must be a bridge in the subgraph.
 *
 * 2nd part: If an edge e is a bridge in the subgraph containing all edges with weights less than or equal to its
 * weight then e is critical.
 * Consider by contradiction that e is not critical. If e is not critical, then there must be another edge that
 * could replace it in an MST and would not cause the MST weight to increase.
 * However, if this edge existed, it would be part of the subgraph containing all edges with weights
 * less than or equal to the weight of edge e. It would also connect both components C1 and C2 that are connected
 * by edge e. However, e is a bridge and its removal would split components C1 and C2. So no such edge exists.
 * Therefore, edge e is critical.
 */
// O(E lg E)
public Queue<Edge> findCriticalEdges(EdgeWeightedGraph edgeWeightedGraph) {
    Queue<Edge> criticalEdges = new Queue<>();
    // Modified Kruskal's algorithm
    Queue<Edge> minimumSpanningTree = new Queue<>();
    PriorityQueueResize<Edge> priorityQueue = new PriorityQueueResize<>(PriorityQueueResize.Orientation.MIN);
    for (Edge edge : edgeWeightedGraph.edges()) {
        priorityQueue.insert(edge);
    }
    UnionFind unionFind = new UnionFind(edgeWeightedGraph.vertices());
    // Subgraph with components
    EdgeWeightedGraphWithDelete componentsSubGraph = new EdgeWeightedGraphWithDelete(unionFind.count());
    while (!priorityQueue.isEmpty() && minimumSpanningTree.size() < edgeWeightedGraph.vertices() - 1) {
        Edge edge = priorityQueue.deleteTop();
        int vertex1 = edge.either();
        int vertex2 = edge.other(vertex1);
        // Ineligible edges are never critical edges
        if (unionFind.connected(vertex1, vertex2)) {
            continue;
        }
        // Get next equal-weight edge block
        double currentWeight = edge.weight();
        HashSet<Edge> equalWeightEdges = new HashSet<>();
        equalWeightEdges.add(edge);
        while (!priorityQueue.isEmpty() && priorityQueue.peek().weight() == currentWeight) {
            equalWeightEdges.add(priorityQueue.deleteTop());
        }
        if (equalWeightEdges.size() == 1) {
            // There is no cycle, so this is a critical edge
            criticalEdges.enqueue(edge);
            unionFind.union(vertex1, vertex2);
            minimumSpanningTree.enqueue(edge);
            continue;
        }
        List<Edge> edgesToAddToComponentsSubGraph = new ArrayList<>();
        // Map to make the mapping between edges in the components subgraph and the original graph
        int averageMapListSize = Math.max(2, equalWeightEdges.size() / 20);
        SeparateChainingHashTable<Edge, Edge> subGraphToGraphEdgeMap = new SeparateChainingHashTable<>(equalWeightEdges.size(), averageMapListSize);
        HashSet<Integer> verticesInSubGraph = new HashSet<>();
        // Generate subgraph with the current components
        for (Edge edgeInCurrentBlock : equalWeightEdges.keys()) {
            vertex1 = edgeInCurrentBlock.either();
            vertex2 = edgeInCurrentBlock.other(vertex1);
            int component1 = unionFind.find(vertex1);
            int component2 = unionFind.find(vertex2);
            Edge subGraphEdge = new Edge(component1, component2, currentWeight);
            edgesToAddToComponentsSubGraph.add(subGraphEdge);
            subGraphToGraphEdgeMap.put(subGraphEdge, edgeInCurrentBlock);
            verticesInSubGraph.add(component1);
            verticesInSubGraph.add(component2);
        }
        for (Edge edgeToAddToComponentSubGraph : edgesToAddToComponentsSubGraph) {
            componentsSubGraph.addEdge(edgeToAddToComponentSubGraph);
        }
        // Run DFS to check if there is a cycle. Any edges in the cycle are non-critical.
        // Every edge in the original graph will be visited by a DFS at most once.
        HashSet<Edge> nonCriticalEdges = new HashSet<>();
        // Use a different constructor for EdgeWeightedCycle to avoid O(E * V) runtime
        EdgeWeightedCycle edgeWeightedCycle = new EdgeWeightedCycle(componentsSubGraph, verticesInSubGraph);
        if (edgeWeightedCycle.hasCycle()) {
            for (Edge edgeInCycle : edgeWeightedCycle.cycle()) {
                Edge edgeInGraph = subGraphToGraphEdgeMap.get(edgeInCycle);
                nonCriticalEdges.add(edgeInGraph);
            }
        }
        // Clear components subgraph edges
        for (Edge edgeToAddToComponentSubGraph : edgesToAddToComponentsSubGraph) {
            componentsSubGraph.deleteEdge(edgeToAddToComponentSubGraph);
        }
        // Add all edges that belong to an MST to the MST
        for (Edge edgeInCurrentBlock : equalWeightEdges.keys()) {
            if (!nonCriticalEdges.contains(edgeInCurrentBlock)) {
                criticalEdges.enqueue(edgeInCurrentBlock);
            }
            vertex1 = edgeInCurrentBlock.either();
            vertex2 = edgeInCurrentBlock.other(vertex1);
            if (!unionFind.connected(vertex1, vertex2)) {
                unionFind.union(vertex1, vertex2);
                // Add edge to the minimum spanning tree
                minimumSpanningTree.enqueue(edge);
            }
        }
    }
    return criticalEdges;
}
Also used : PriorityQueueResize(chapter2.section4.PriorityQueueResize) ArrayList(java.util.ArrayList) UnionFind(chapter1.section5.UnionFind) SeparateChainingHashTable(chapter3.section4.SeparateChainingHashTable) Queue(chapter1.section3.Queue) HashSet(chapter3.section5.HashSet)

Example 22 with Queue

use of com.google.cloud.tasks.v2.Queue in project algorithms-sedgewick-wayne by reneargento.

the class Exercise44_WebCrawler method crawlWeb.

public void crawlWeb(String sourceWebPage) {
    // timeout connection after 500 milliseconds
    System.setProperty("sun.net.client.defaultConnectTimeout", "500");
    System.setProperty("sun.net.client.defaultReadTimeout", "1000");
    Queue<String> webPagesQueue = new Queue<>();
    webPagesQueue.enqueue(sourceWebPage);
    HashSet<String> visited = new HashSet<>();
    visited.add(sourceWebPage);
    while (!webPagesQueue.isEmpty()) {
        String currentWebPage = webPagesQueue.dequeue();
        String webPageContent;
        try {
            In in = new In(currentWebPage);
            webPageContent = in.readAll();
        } catch (IllegalArgumentException exception) {
            StdOut.println("Could not open " + currentWebPage);
            continue;
        }
        StdOut.println(currentWebPage + " crawled");
        /**
         ***********************************************************
         *  Find links of the form: http://xxx.yyy.zzz or https://xxx.yyy.zzz
         *  s? for either http or https
         *  \\w+ for one or more alpha-numeric characters
         *  \\. for dot
         ************************************************************
         */
        String regexp = "http(s?)://(\\w+\\.)+(\\w+)";
        Pattern pattern = Pattern.compile(regexp);
        Matcher matcher = pattern.matcher(webPageContent);
        // Find all matches
        while (matcher.find()) {
            String webPage = matcher.group();
            if (!visited.contains(webPage)) {
                webPagesQueue.enqueue(webPage);
                visited.add(webPage);
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) In(edu.princeton.cs.algs4.In) Matcher(java.util.regex.Matcher) Queue(chapter1.section3.Queue) HashSet(chapter3.section5.HashSet)

Example 23 with Queue

use of com.google.cloud.tasks.v2.Queue in project java-docs-samples by GoogleCloudPlatform.

the class PurgeQueue method purgeQueue.

public static void purgeQueue(String projectId, String locationId, String queueId) throws Exception {
    try (CloudTasksClient client = CloudTasksClient.create()) {
        // TODO(developer): Uncomment these lines and replace with your values.
        // String projectId = "your-project-id";
        // String locationId = "us-central1";
        // String queueId = "foo";
        // Construct the fully qualified queue name.
        String queueName = QueueName.of(projectId, locationId, queueId).toString();
        client.purgeQueue(queueName);
        System.out.println("Queue Purged.");
    }
}
Also used : CloudTasksClient(com.google.cloud.tasks.v2.CloudTasksClient)

Example 24 with Queue

use of com.google.cloud.tasks.v2.Queue in project java-docs-samples by GoogleCloudPlatform.

the class CreateTask method main.

public static void main(String... args) throws Exception {
    Options options = new Options();
    options.addOption(PROJECT_ID_OPTION);
    options.addOption(QUEUE_OPTION);
    options.addOption(LOCATION_OPTION);
    options.addOption(PAYLOAD_OPTION);
    options.addOption(IN_SECONDS_OPTION);
    if (args.length == 0) {
        printUsage(options);
        return;
    }
    CommandLineParser parser = new DefaultParser();
    CommandLine params = null;
    try {
        params = parser.parse(options, args);
    } catch (ParseException e) {
        System.err.println("Invalid command line: " + e.getMessage());
        printUsage(options);
        return;
    }
    String projectId;
    if (params.hasOption("project-id")) {
        projectId = params.getOptionValue("project-id");
    } else {
        projectId = System.getenv(GOOGLE_CLOUD_PROJECT_KEY);
    }
    if (Strings.isNullOrEmpty(projectId)) {
        printUsage(options);
        return;
    }
    String queueName = params.getOptionValue(QUEUE_OPTION.getOpt());
    String location = params.getOptionValue(LOCATION_OPTION.getOpt());
    String payload = params.getOptionValue(PAYLOAD_OPTION.getOpt(), "default payload");
    // Instantiates a client.
    try (CloudTasksClient client = CloudTasksClient.create()) {
        // Variables provided by the CLI.
        // projectId = "my-project-id";
        // queueName = "my-appengine-queue";
        // location = "us-central1";
        // payload = "hello";
        // Construct the fully qualified queue name.
        String queuePath = QueueName.of(projectId, location, queueName).toString();
        // Construct the task body.
        Task.Builder taskBuilder = Task.newBuilder().setAppEngineHttpRequest(AppEngineHttpRequest.newBuilder().setBody(ByteString.copyFrom(payload, Charset.defaultCharset())).setRelativeUri("/tasks/create").setHttpMethod(HttpMethod.POST).build());
        if (params.hasOption(IN_SECONDS_OPTION.getOpt())) {
            // Add the scheduled time to the request.
            int seconds = Integer.parseInt(params.getOptionValue(IN_SECONDS_OPTION.getOpt()));
            taskBuilder.setScheduleTime(Timestamp.newBuilder().setSeconds(Instant.now(Clock.systemUTC()).plusSeconds(seconds).getEpochSecond()));
        }
        // Send create task request.
        Task task = client.createTask(queuePath, taskBuilder.build());
        System.out.println("Task created: " + task.getName());
    }
// [END cloud_tasks_appengine_create_task]
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) Task(com.google.cloud.tasks.v2.Task) CloudTasksClient(com.google.cloud.tasks.v2.CloudTasksClient) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) ByteString(com.google.protobuf.ByteString) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 25 with Queue

use of com.google.cloud.tasks.v2.Queue in project nomulus by google.

the class TldFanoutAction method run.

@Override
public void run() {
    checkArgument(!(runInEmpty && (forEachTestTld || forEachRealTld)), "runInEmpty and forEach*Tld are mutually exclusive");
    checkArgument(runInEmpty || forEachTestTld || forEachRealTld, "At least one of runInEmpty, forEachTestTld, forEachRealTld must be given");
    checkArgument(!(runInEmpty && !excludes.isEmpty()), "Can't specify 'exclude' with 'runInEmpty'");
    ImmutableSet<String> tlds = runInEmpty ? ImmutableSet.of("") : Streams.concat(forEachRealTld ? getTldsOfType(REAL).stream() : Stream.of(), forEachTestTld ? getTldsOfType(TEST).stream() : Stream.of()).filter(not(in(excludes))).collect(toImmutableSet());
    Multimap<String, String> flowThruParams = filterKeys(params, not(in(CONTROL_PARAMS)));
    StringBuilder outputPayload = new StringBuilder(String.format("OK: Launched the following %d tasks in queue %s\n", tlds.size(), queue));
    logger.atInfo().log("Launching %d tasks in queue %s.", tlds.size(), queue);
    if (tlds.isEmpty()) {
        logger.atWarning().log("No TLDs to fan-out!");
    }
    for (String tld : tlds) {
        Task task = createTask(tld, flowThruParams);
        Task createdTask = cloudTasksUtils.enqueue(queue, task);
        outputPayload.append(String.format("- Task: '%s', tld: '%s', endpoint: '%s'\n", createdTask.getName(), tld, createdTask.getAppEngineHttpRequest().getRelativeUri()));
        logger.atInfo().log("Task: '%s', tld: '%s', endpoint: '%s'.", createdTask.getName(), tld, createdTask.getAppEngineHttpRequest().getRelativeUri());
    }
    response.setContentType(PLAIN_TEXT_UTF_8);
    response.setPayload(outputPayload.toString());
}
Also used : Task(com.google.cloud.tasks.v2.Task)

Aggregations

CloudTasksClient (com.google.cloud.tasks.v2.CloudTasksClient)10 Task (com.google.cloud.tasks.v2.Task)8 In (edu.princeton.cs.algs4.In)5 Queue (edu.princeton.cs.algs4.Queue)5 Queue (chapter1.section3.Queue)4 LocationName (com.google.cloud.tasks.v2.LocationName)3 Queue (com.google.cloud.tasks.v2.Queue)3 ByteString (com.google.protobuf.ByteString)3 Test (org.junit.jupiter.api.Test)3 HashSet (chapter3.section5.HashSet)2 AppEngineHttpRequest (com.google.cloud.tasks.v2.AppEngineHttpRequest)2 Transaction (edu.princeton.cs.algs4.Transaction)2 StringJoiner (java.util.StringJoiner)2 UnionFind (chapter1.section5.UnionFind)1 PriorityQueueResize (chapter2.section4.PriorityQueueResize)1 SeparateChainingHashTable (chapter3.section4.SeparateChainingHashTable)1 ApiCallContext (com.google.api.gax.rpc.ApiCallContext)1 UnaryCallable (com.google.api.gax.rpc.UnaryCallable)1 CreateTaskRequest (com.google.cloud.tasks.v2.CreateTaskRequest)1 QueueName (com.google.cloud.tasks.v2.QueueName)1