Using Golang Iterators for Efficiency

TL;DR In this blog post, we will see how new golang iterators1 enables us to write efficient code. Although, we talk about iterators in golang, same argument holds for other languages as well2. Traditional Approach Consider a hypothetical example of writing powerset function to generate all possible subsets: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 // We are using slice instead of set func PowerSet[T any](values []T) [][]T { if len(values) == 0 { return nil } results := make([][]T, 0) for i := 0; i < int(math.Pow(2, float64(len(values)))); i++ { result := make([]T, 0) for j := 0; j < len(values); j++ { if i&(1<<j) != 0 { result = append(result, values[j]) } } results = append(results, result) } return results } What is going on here: ...

November 10, 2024 · 581 words · Vivek Akupatni

Evaluating Alternate Variations of Kosaraju Sharir Algorithm for Computing Strongly Connected Components

TL;DR In this blog post, we will explore examples illustrating why alternate variations of Kosaraju-Sharir’s algorithm for computing strongly connected components fail. Prerequisites I assume that reader is already familiar with the below topics: Strongly connected component problem Rough idea of Kosaraju Sharir Algorithm1. Finally, a handy reference to clrs book as we will be using the conceptual framework from this book. Terminology Discovery time of a vertex: When a vertex is first discovered. Denoted as v.discovery finish time of a vertex: When the vertex is fully explored and recursive dfs function for the vertex returns. Denoted as v.finish Conventions used: An edge being explored is in bold red color and an edge in blue color is part of dfs tree. Time is monotonically increasing and it starts from 1. discovery_time/finish_time times appear below the node. Nodes are that fully processed are in blue color and currently being explored are in red color. Consider an example of running dfs from a vertex a ...

February 3, 2024 · 621 words · Vivek Akupatni

Thoughts on GoLang - Part 1

Couple of years ago, one of my friends wanted me to look at golang. Back then, golang was a shiny language and there was a lot of hype around it. I briefly look at the language and I had two primary concerns back then: Lack of generics: No longer exists now as generics were added in go 1.181. Better ergonomic error handling: This problem even exists at the time of writing. Most of the times I see a common pattern mentioned below: // common pattern result, err := fetch_value() if err != nil { return nil, err } ​ I wish there was a way2 in which I can return an error if I don’t want to deal with it. Something along the lines of: ...

March 26, 2023 · 596 words · Vivek Akupatni