使用Boost.Graph来解决最长的路径

时间:2016-05-25 10:26:03

标签: c++ graph boost-graph longest-path

我正在尝试找到一种方法来解决具有以下特征的图表的longest path problem

  • 定向
  • 加权(包括负重量)
  • 非循环,但我只是在寻找简单路径

过去两天我第一次看过Boost.Graph。它似乎非常复杂,我想确保我可以解决我的问题,然后我深入研究文档,发现我实际上无法使用它。

那么我可以使用Boost.Graph来解决我的问题吗?它甚至可能是NP-Complete?即使我可以使用Boost.Graph,基于性能会有更好的选择吗?

1 个答案:

答案 0 :(得分:2)

下面是一些使用boost图库来查找图中两个顶点之间最长(加权最重)路径的代码。检查图表的周期,通过删除图表副本中的后边缘来删除这些周期。

此代码的重构和文档版本可从https://chiselapp.com/user/ravenspoint/repository/longest_path/dir?ci=tip的公共化石库获取

import (
    "bufio"
    "fmt"
    "os"
    "strings"
)

func ValidLines(filename string) (c chan string) {
    c = make(chan string)
    buff := ""
    go func() {
        file, err := os.Open(filename)
        if err != nil {
            close(c)
            return
        }

        reader := bufio.NewReader(file)
        for {
            line, err := reader.ReadString('\n')
            if err != nil {
                close(c)
                return
            }
            line = strings.TrimSpace(line)
            if line == "" {
                continue
            }
            buff += line
            if line[len(line)-1] != ';' {
                continue
            }
            c <- buff
            buff = ""
        }
    }()
    return c
}

func main() {
    for line := range ValidLines("myfile.txt") {
        fmt.Println(line)
    }
}