Skip to content

MappedGraph and ShortestAll / LongestAll functions drops distance 0 instead of sum of arcs value. #25

@janmpo

Description

@janmpo

Hi,

I'm starting using this project and I'm facing some issues with this code:

package main

import (
	"fmt"
	"log"

	"github.com/RyanCarrier/dijkstra/v2"
)

func main() {
	dijkstraMapped()
}

func dijkstraMapped() {
	graph := dijkstra.NewMappedGraph[string]()
	// Add  verticies
	graph.AddEmptyVertex("A")
	graph.AddEmptyVertex("B")
	graph.AddEmptyVertex("C")
	graph.AddEmptyVertex("D")
	graph.AddEmptyVertex("E")
	// Add Arcs
	err := graph.AddVertexAndArcs("A", map[string]uint64{"B": 1, "C": 1})
	if err != nil {
		log.Fatal(err)
	}
	err = graph.AddVertexAndArcs("B", map[string]uint64{"D": 1})
	if err != nil {
		log.Fatal(err)
	}
	err = graph.AddVertexAndArcs("C", map[string]uint64{"D": 1})
	if err != nil {
		log.Fatal(err)
	}
	err = graph.AddVertexAndArcs("D", map[string]uint64{"E": 1})
	if err != nil {
		log.Fatal(err)
	}

	shortest, err := graph.Shortest("A", "E")
	if err != nil {
		fmt.Println(graph)
		log.Fatal(err)
	}
	fmt.Println("Shortest distance is", shortest.Distance, "following path: ", shortest.Path)

	longest, err := graph.Longest("A", "E")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("Longest distance is", longest.Distance, "following path: ", longest.Path)
	shortests, err := graph.ShortestAll("A", "E")
	if err != nil {
		fmt.Println(graph)
		log.Fatal(err)
	}
	fmt.Println("ShortestAll distance is", shortests.Distance, "following paths: ", shortests.Paths)

	longests, err := graph.LongestAll("A", "E")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("LongestAll distance is", longests.Distance, "following paths: ", longests.Paths)
}

With the following result:

Shortest distance is 3 following path:  [A B D E]
Longest distance is 3 following path:  [A B D E]
ShortestAll distance is 0 following paths:  [[A B D E] [A C D E]]
LongestAll distance is 0 following paths:  [[A C D E] [A B D E]]

My question is: why when I compute ShortestAll and LongestAll the distance is 0?

Thanks for your support

Juan

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions