Last active
September 3, 2017 07:03
-
-
Save kanapuli/d7d673dd5b4c5f68d8390e3fa0cd56f2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//topological sort | |
package main | |
import ( | |
"fmt" | |
"sort" | |
) | |
var pathOfFullstack = map[string][]string{ | |
"fullstack developer": {"C#", "Python", "Go", "Angular", "React"}, | |
"C#": {"C"}, | |
"Angular": {"html", "Css", "Javascript"}, | |
"Go": {"C#", "Python"}, | |
"React": {"html", "Javascript", "JSX"}, | |
} | |
func main() { | |
for i, path := range topoSort(pathOfFullstack) { | |
fmt.Printf("%d:\t%s\n", i+1, path) | |
} | |
} | |
func topoSort(m map[string][]string) []string { | |
var pathOrder []string | |
visited := make(map[string]bool) | |
var visitAll func(items []string) | |
visitAll = func(items []string) { | |
for _, item := range items { | |
if !visited[item] { | |
visited[item] = true | |
visitAll(m[item]) | |
pathOrder = append(pathOrder, item) | |
} | |
} | |
} | |
var paths []string | |
for devPath := range m { | |
paths = append(paths, devPath) | |
} | |
sort.Strings(paths) | |
visitAll(paths) | |
return pathOrder | |
} | |
// Output | |
/* | |
1: html | |
2: Css | |
3: Javascript | |
4: Angular | |
5: C | |
6: C# | |
7: Python | |
8: Go | |
9: JSX | |
10: React | |
11: fullstack developer | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment