From ba0027913467957d08561a012c838e00b313238f Mon Sep 17 00:00:00 2001 From: Ionut Hodoroaga Date: Wed, 25 Sep 2024 10:32:40 +0300 Subject: [PATCH] [ENHANCEMENT] Include Time and Space Complexity Information #698 Added time and space complexity for Bellman-Ford, find cycle and depth first search implementations --- graph/bellmanford.go | 2 ++ graph/cycle.go | 4 ++++ graph/depthfirstsearch.go | 3 +++ 3 files changed, 9 insertions(+) diff --git a/graph/bellmanford.go b/graph/bellmanford.go index f130a4e06..7831df633 100644 --- a/graph/bellmanford.go +++ b/graph/bellmanford.go @@ -3,6 +3,8 @@ // It is slower than Dijkstra but capable of handling negative edge weights. // https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm // Implementation is based on the book 'Introduction to Algorithms' (CLRS) +// Worst Case Time Complexity: O(V * E) +// Auxiliary Space: O(V) package graph diff --git a/graph/cycle.go b/graph/cycle.go index cf45d1854..df3572a7f 100644 --- a/graph/cycle.go +++ b/graph/cycle.go @@ -5,6 +5,8 @@ package graph +// Worst Case Time Complexity: O(V + E) +// Auxiliary Space: O(V) func (g *Graph) HasCycle() bool { //this implimetation referred as 3-color too all := map[int]struct{}{} @@ -45,6 +47,8 @@ func (g Graph) hasCycleHelper(v int, all, visiting, visited map[int]struct{}) bo } // this function can do HasCycle() job but it is slower +// Worst Case Time Complexity: O(V + E) +// Auxiliary Space: O(V) func (g *Graph) FindAllCycles() []Graph { all := map[int]struct{}{} visiting := map[int]struct{}{} diff --git a/graph/depthfirstsearch.go b/graph/depthfirstsearch.go index c035c79f5..7d06d0f22 100644 --- a/graph/depthfirstsearch.go +++ b/graph/depthfirstsearch.go @@ -1,3 +1,6 @@ +// Worst Case Time Complexity: O(V + E) +// Auxiliary Space: O(V) + package graph func GetIdx(target int, nodes []int) int {