From fed5ae317e5cf042d87537fc02d9ccaaf6e6022a Mon Sep 17 00:00:00 2001 From: Harish1604 <144940633+Harish1604@users.noreply.github.com> Date: Mon, 23 Dec 2024 21:09:06 +0530 Subject: [PATCH 1/2] Written complete Tarjan's Algorithm in GO language --- non_linear/graphs/TarjanAlgorithm.go | 98 ++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 non_linear/graphs/TarjanAlgorithm.go diff --git a/non_linear/graphs/TarjanAlgorithm.go b/non_linear/graphs/TarjanAlgorithm.go new file mode 100644 index 0000000..fdf466b --- /dev/null +++ b/non_linear/graphs/TarjanAlgorithm.go @@ -0,0 +1,98 @@ +package main + +import "fmt" + +type Graph struct { + vertices int + adjList map[int][]int +} + +func NewGraph(vertices int) *Graph { + return &Graph{ + vertices: vertices, + adjList: make(map[int][]int), + } +} + +func (g *Graph) AddEdge(v, w int) { + g.adjList[v] = append(g.adjList[v], w) +} + +func (g *Graph) TarjanSCC() [][]int { + index := 0 + indices := make([]int, g.vertices) + lowLink := make([]int, g.vertices) + onStack := make([]bool, g.vertices) + stack := []int{} + result := [][]int{} + + for i := 0; i < g.vertices; i++ { + indices[i] = -1 + } + + var strongConnect func(v int) + strongConnect = func(v int) { + indices[v] = index + lowLink[v] = index + index++ + stack = append(stack, v) + onStack[v] = true + + for _, w := range g.adjList[v] { + if indices[w] == -1 { + strongConnect(w) + lowLink[v] = min(lowLink[v], lowLink[w]) + } else if onStack[w] { + lowLink[v] = min(lowLink[v], indices[w]) + } + } + + if lowLink[v] == indices[v] { + scc := []int{} + for { + w := stack[len(stack)-1] + stack = stack[:len(stack)-1] + onStack[w] = false + scc = append(scc, w) + if w == v { + break + } + } + result = append(result, scc) + } + } + + for i := 0; i < g.vertices; i++ { + if indices[i] == -1 { + strongConnect(i) + } + } + + return result +} + +func min(a, b int) int { + if a < b { + return a + } + return b +} + +func main() { + g := NewGraph(8) + g.AddEdge(0, 1) + g.AddEdge(1, 2) + g.AddEdge(2, 0) + g.AddEdge(3, 4) + g.AddEdge(4, 5) + g.AddEdge(5, 3) + g.AddEdge(6, 4) + g.AddEdge(6, 7) + g.AddEdge(7, 6) + + fmt.Println("Strongly Connected Components:") + sccs := g.TarjanSCC() + for i, scc := range sccs { + fmt.Printf("SCC %d: %v\n", i+1, scc) + } +} From 5c29bc31894aac122ec51850dfa4aa3987840d47 Mon Sep 17 00:00:00 2001 From: Harish1604 <144940633+Harish1604@users.noreply.github.com> Date: Tue, 24 Dec 2024 20:54:01 +0530 Subject: [PATCH 2/2] Coded radix tree implementation in go lang, clearly --- non_linear/trie/radix_tree.go | 97 +++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 non_linear/trie/radix_tree.go diff --git a/non_linear/trie/radix_tree.go b/non_linear/trie/radix_tree.go new file mode 100644 index 0000000..3c45a02 --- /dev/null +++ b/non_linear/trie/radix_tree.go @@ -0,0 +1,97 @@ +package radixtree +type Node struct { + Key rune + Children map[rune]*Node + IsComplete bool +} + + +func NewNode(key rune) *Node { + return &Node{ + Key: key, + Children: make(map[rune]*Node), + } +} + + +func (n *Node) AddChild(key rune, isComplete bool) *Node { + child, exists := n.Children[key] + if !exists { + child = NewNode(key) + n.Children[key] = child + } + if isComplete { + child.IsComplete = true + } + return child +} + + +func (n *Node) GetChild(key rune) *Node { + return n.Children[key] +} + + +func (n *Node) RemoveChild(key rune) { + delete(n.Children, key) +} +type RadixTree struct { + Head *Node +} + + +func NewRadixTree() *RadixTree { + return &RadixTree{ + Head: NewNode('*'), + } +} + + +func (t *RadixTree) Insert(word string) { + currentNode := t.Head + for i, char := range word { + isComplete := i == len(word)-1 + currentNode = currentNode.AddChild(char, isComplete) + } +} + + +func (t *RadixTree) Delete(word string) { + var depthFirstDelete func(currentNode *Node, index int) + depthFirstDelete = func(currentNode *Node, index int) { + if index >= len(word) { + return + } + char := rune(word[index]) + nextNode := currentNode.GetChild(char) + if nextNode == nil { + return + } + depthFirstDelete(nextNode, index+1) + if index == len(word)-1 { + nextNode.IsComplete = false + } + if len(nextNode.Children) == 0 && !nextNode.IsComplete { + currentNode.RemoveChild(char) + } + } + depthFirstDelete(t.Head, 0) +} + + +func (t *RadixTree) Find(word string) bool { + node := t.getLastCharacterNode(word) + return node != nil && node.IsComplete +} + + +func (t *RadixTree) getLastCharacterNode(word string) *Node { + currentNode := t.Head + for _, char := range word { + currentNode = currentNode.GetChild(char) + if currentNode == nil { + return nil + } + } + return currentNode +}