diff options
| author | ruki <[email protected]> | 2025-03-21 22:41:46 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-04-08 15:31:54 +0800 |
| commit | 7e949212c4aff661f6dfd6234af7fd021a8568e3 (patch) | |
| tree | 37d4602fe235087bf7d6cb9aed87b3a3e6c13360 /xmake/core/base/graph.lua | |
| parent | 0662b04580e06e6d18b254e4005869569d8a8c50 (diff) | |
remove dfs
Diffstat (limited to 'xmake/core/base/graph.lua')
| -rw-r--r-- | xmake/core/base/graph.lua | 75 |
1 files changed, 12 insertions, 63 deletions
diff --git a/xmake/core/base/graph.lua b/xmake/core/base/graph.lua index 681d390c3..ffcd815c5 100644 --- a/xmake/core/base/graph.lua +++ b/xmake/core/base/graph.lua @@ -119,47 +119,19 @@ function graph:remove_vertex(v) end end --- topological sort, use DFS algorithom -function graph:_topological_sort_dfs() - local visited = {} - for _, v in ipairs(self:vertices()) do - visited[v] = false - end - local in_stack = {} - local order_vertices = {} - local function dfs(v) - visited[v] = true - in_stack[v] = true - local edges = self:adjacent_edges(v) - if edges then - for _, e in ipairs(edges) do - local w = e:other(v) - if not visited[w] then - if dfs(w) then - return true - end - elseif in_stack[w] then - return true - end - end - end - in_stack[v] = false - table.insert(order_vertices, v) - end - local has_cycle = false - for _, v in ipairs(self:vertices()) do - if not visited[v] then - if dfs(v) then - has_cycle = true - break - end - end - end - return table.reverse(order_vertices), has_cycle -end - -- topological sort, use Kahn's algorithm -function graph:_topological_sort_kahn() +-- +-- e.g. +-- +-- add_edge(a, b) -- a depend on b +-- add_edge(b, c) -- b depend on c +-- +-- it will return {c, b, a} +function graph:topological_sort(opt) + opt = opt or {} + if not self:is_directed() then + return + end -- calculate in-degree for each vertex local in_degree = {} @@ -219,29 +191,6 @@ function graph:_topological_sort_kahn() return order_vertices, has_cycle end --- topological sort (default: Kahn's algorithm) --- --- @param opt the options, we can use `{algorithm = "dfs/kahn"}` to select sort algorithm, --- and the Kahn is the default algorithm. --- --- e.g. --- --- add_edge(a, b) -- a depend on b --- add_edge(b, c) -- b depend on c --- --- it will return {c, b, a} -function graph:topological_sort(opt) - opt = opt or {} - if not self:is_directed() then - return - end - if opt.algorithm == "dfs" then - return self:_topological_sort_dfs() - else - return self:_topological_sort_kahn() - end -end - -- find cycle function graph:find_cycle() local visited = {} |
