{"uid":"cap_QWdlsLccWL-NuOiwsNgYD","slug":"graph-algorithms-topological-sort-kahn-with-parallel-layers-25056efd","name":"Graph Algorithms – Topological Sort (Kahn with Parallel Layers)","description":"Topological order of a directed graph (Kahn) with parallel layers; on a cycle returns the cycle's nodes.","url":"https://graph-algorithms.x402tookit.com/topological-sort?utm_source=zero.xyz","method":"GET","headers":{},"bodySchema":{"type":"object","$schema":"https://json-schema.org/draft/2020-12/schema","required":["input"],"properties":{"input":{"type":"object","required":["type","method"],"properties":{"type":{"type":"string","const":"http"},"method":{"enum":["GET"],"type":"string"},"queryParams":{"type":"object","required":["edges"],"properties":{"edges":{"type":"string","description":"Edges separated by ; or newline, each 'from,to' or 'from,to,weight' (default weight 1), or a JSON array of [from,to,weight]; up to 200 nodes, 1000 edges, 60000 characters; node names 1-32 characters of letters, digits, _ . : + -. Each edge 'a,b' means a must come before b (weights ignored)"},"nodes":{"type":"string","description":"Optional extra node names (comma or space separated) so isolated nodes are counted; the total still counts toward the 200-node cap"}}}},"additionalProperties":false},"output":{"type":"object","required":["type"],"properties":{"type":{"type":"string"},"example":{"type":"object","properties":{"asOf":{"type":["null","string"]},"cycle":{},"order":{"type":["array","null"],"items":{"type":"string"}},"layers":{"type":["array","null"],"items":{"type":"array","items":{"type":"string"}}},"source":{"type":["null","string"]},"reasons":{"type":["array","null"],"items":{"type":"string"}},"summary":{"type":["null","string"]},"verdict":{"type":["null","string"]},"degraded":{"type":["boolean","null"]},"edgeCount":{"type":["null","number"]},"nodeCount":{"type":["null","number"]},"disclaimer":{"type":["null","string"]},"notChecked":{"type":["array","null"],"items":{"type":"string"}},"criticalPathLength":{"type":["null","number"]}}}}}}},"responseSchema":null,"example":null,"exampleRequest":null,"tags":["x402"],"displayCostAmount":"0.02","displayCostAsset":"USDC","priceDynamic":false,"priceHint":null,"priceStatus":"priced","priceSource":"probe","requiresHandshake":false,"reviewCount":0,"rating":{"score":"0.00","successRate":"0.00","reviews":0,"stars":null,"state":"unrated"},"availabilityStatus":"unknown","priceObserved":null,"sessionDeposit":null,"pricing":{"kind":"static","summary":"$0.02/call","primary":{"kind":"static","protocol":"x402","network":"base","amountUsd":"0.02","per":"call","confidence":"exact"},"accepted":[{"kind":"static","protocol":"x402","network":"base","amountUsd":"0.02","per":"call","confidence":"exact"}]},"paymentMethods":[{"uid":"pm_GWNvZe_G_ohylA-4WmTa5","protocol":"x402","methodType":"crypto","chain":"base","mode":"charge","costAmount":"0.02","costPer":"request","priority":0,"asset":"0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913","unit":"request","depositMicros":null,"planRef":null}],"brandName":null,"brandSlug":null,"brandBaseUrl":null,"brandDocsUrl":null,"whatItDoes":"Computes a topological ordering of a directed graph using Kahn's algorithm, grouping nodes into parallel execution layers; if a cycle exists, returns the cycle's nodes instead.","exampleAgentPrompt":"I have these task dependencies — 'compile,link; link,test; build,compile; build,package' — can you give me a topological order and tell me which tasks can run in parallel?","exampleUseCases":[{"title":"CI/CD pipeline stage ordering","prompt":"I have these build steps with dependencies: 'checkout,compile; compile,test; compile,lint; test,package; lint,package; package,deploy' — give me a topological order and show me which stages can run in parallel so I can optimize my pipeline."},{"title":"Course prerequisite scheduling","prompt":"I need to figure out the order to take these courses: 'math101,math201; math201,algorithms; algorithms,ml; math101,stats; stats,ml' — what's the right sequence and are there any circular prerequisites?"},{"title":"Microservice startup order","prompt":"My microservices have these startup dependencies: 'db,auth; db,cache; auth,api; cache,api; api,frontend; config,db; config,cache' — what order should they start in, and which ones can launch at the same time?"}],"resultDescription":"Returns a flat topological order array of node names, a layers array grouping nodes that can execute in parallel (each layer is an array of node names), node count, edge count, critical path length, and an asOf timestamp. If the graph contains a cycle, returns the cycle's nodes instead of an order, along with a summary and reasons explaining the cycle detection.","failureModes":["Cycle detected — returns cycle node list instead of topological order","Graph exceeds 200 nodes or 1000 edges — request rejected","Edge string malformed or unparseable — parsing error returned","Node names exceed 32 characters or contain invalid characters — rejected","Query string exceeds 60000 characters — request rejected","Network timeout or service unavailable — HTTP 5xx error","Payment not completed — HTTP 402 response requiring USDC payment"],"whenToPreferThis":"Use this endpoint when you need to determine a valid execution order for nodes in a directed acyclic graph, especially when you also want to know which nodes can be processed in parallel (layers). It also doubles as a cycle detector for directed graphs — if a topological order cannot be computed, the cycle is reported. Prefer this over Tarjan/SCC endpoints when your primary goal is ordering rather than component membership, and over Floyd-Warshall or Dijkstra when you don't need shortest paths but just dependency ordering.","instructions":null,"reviewSummary":null,"reviewSummaryHighlights":null,"reviewSummaryConcerns":null,"reviewSummaryGeneratedAt":null,"activationCount":0,"lastUsedAt":null,"lastSuccessfullyRanAt":null,"lastHealthCheckAt":"2026-11-02T12:31:12.067Z","isFirstParty":false,"canonicalSlug":"graph-algorithms-topological-sort-kahn-with-parallel-layers-25056efd"}