Skip to content

Commit

Permalink
Updated based on code review
Browse files Browse the repository at this point in the history
Signed-off-by: nathannaveen <42319948+nathannaveen@users.noreply.github.com>
  • Loading branch information
nathannaveen committed Aug 9, 2023
1 parent fc6ebca commit 82511e2
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions pkg/guacanalytics/toposort.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,31 @@ import (
func TopoSortFromBfsNodeMap(ctx context.Context, gqlClient graphql.Client, nodeMap map[string]BfsNode) (map[int][]string, []string, error) {
sortedNodes := make(map[int][]string) // map of level -> list of nodeIDs at that level
parentsMap, childrensMap, infoNodes := copyParents(nodeMap)
// parentsMap: map of nodeID (child) -> list of parents in the form of a map
// childrensMap: map of nodeID (parent) -> list of children in the form of a list
// parentsMap: map of nodeID (child) -> the struct parent which contains a list of parents in the form of a map
// childrensMap: map of nodeID (parent) -> list of children in the form of an array
bfsLevel := 0
numNodes := 0
totalNodes := len(parentsMap)

for numNodes < totalNodes {
foundIDs := make(map[string]bool)
for id, pMap := range parentsMap {
if pMap.parents != nil && len(pMap.parents) == 0 { // if this node has no parents, it is a root node
for id, p := range parentsMap {
if p.parents != nil && len(p.parents) == 0 { // if this node has no parents, it is a root node
sortedNodes[bfsLevel] = append(sortedNodes[bfsLevel], id)
numNodes++
foundIDs[id] = true
delete(parentsMap, id)
}
}

for id := range foundIDs {
delete(parentsMap, id) // remove this node from the map of parents
for _, childID := range childrensMap[id] { // loop through all the children of this node
delete(parentsMap[childID].parents, id) // remove this node from the map of parents of the child
}
}

if len(foundIDs) == 0 {
// TODO: print out offending cycle
return sortedNodes, infoNodes, fmt.Errorf("error: cycle detected")
}

Expand Down

0 comments on commit 82511e2

Please sign in to comment.