package main

import (
	"Study/src/study/day7Function/HtmlTools"
	"fmt"
	"log"
)

func main() {
	// 有向无环图
	var prereqs = map[string][]string{
		"algorithms":     {"data structures"},
		"linear algebra": {"calculus"},
		"calculus":       {"linear algebra"},
		"compilers": {
			"data structures",
			"formal languages",
			"computer organization",
		},
		"data structures":       {"discrete math"},
		"databases":             {"data structures"},
		"discrete math":         {"intro to programming"},
		"formal languages":      {"discrete math"},
		"networks":              {"operating systems"},
		"operating systems":     {"data structures", "computer organization"},
		"programming languages": {"data structures", "computer organization"},
	}
	breadthFirstDigraph(prereqs)

}
func breadthFirstDigraph(m map[string][]string) []string {

	var worklist, res []string
	// 将第一层节点加入到队列
	for k, _ := range m {
		worklist = append(worklist, k)
	}
	// seen
	seen := make(map[string]bool)
	for len(worklist) > 0 {
		// 复制当前队列
		items := worklist
		// 清空队列
		worklist = nil
		// 遍历复制的当前队列
		for _, item := range items {
			// 判断seen, 防止重复添加到队列
			if !seen[item] {
				seen[item] = true
				// 添加到队列
				worklist = append(worklist, m[item]...)
				fmt.Println(item)
				res = append(res, item)
			}
		}
	}
	return res
}
func crawl(url string) []string {
	fmt.Println(url)
	list, err := HtmlTools.Extract()
	if err != nil {
		log.Print(err)
	}
	return list
}
