by Jose_Luis_Gomez » Tue Mar 09, 2010 10:21 pm
(* Dear Tugrul: I was able to create a small tutorial about the use of CostOfPath *)
(* This is the first time I use Combinatorica, therefore this is for sure Not the most efficient way to do it*)
(* PLEASE SEE THE MATHEMATICA FILE IN: http://homepage.cem.itesm.mx/lgomez/data/combinatorica.nb *)
(* PLEASE SEE THE PDF FILE IN: http://homepage.cem.itesm.mx/lgomez/data/combinatorica.pdf *)
(* Use of CostOfPath and ShortestPath *)
(* By José Luis Gómez-Muñoz
http://homepage.cem.itesm.mx/lgomez/
Mexico *)
(* FIRST EXAMPLE: Equally Weighted Graph *)
(* Load the package: *)
Needs["Combinatorica`"]
(* Create a simple, equally weighted graph, and store it in the variable myfirstgraph: *)
myfirstgraph = CompleteGraph[6]
(* Show the graph, including vertex numbers: *)
ShowGraph[myfirstgraph, VertexNumber -> True]
(* For this simple graph, the cost of each path *)
(* is just the length of the path (a path is a *)
(* list of vertex numbers). The path {1,3,5,6} is *)
(* the edge 1 to 3, then 3 to 5, then 5 to 6, *)
(* so 3 edges, without weights, the path has a cost of 3: *)
CostOfPath[myfirstgraph, {1, 3, 5, 6}]
(* This is the path. It is made of 3 edges, therefore without weights its total cost is 3: *)
ShowGraph[Highlight[myfirstgraph, {{{1, 3}, {3, 5}, {5, 6}}}],
VertexNumber -> True]
(* SECOND EXAMPLE: Set different weights for the edges *)
(* Load the package: *)
Needs["Combinatorica`"]
(* Create a graph with weights. The weights that *)
(* are Not specified will be considered as "one" by commands like CostOfPath: *)
mysecondgraph = SetGraphOptions[
CompleteGraph[6],
{{{1, 3}, EdgeWeight -> 0.04},
{{3, 5}, EdgeWeight -> 0.08},
{{5, 6}, EdgeWeight -> 0.11},
{{1, 2}, EdgeWeight -> 0.03},
{{2, 5}, EdgeWeight -> 0.07}
}]
(* Weight were defined for some of the edges of the *)
(* graph that was called mysecondgraph. Now the CostOfPath is *)
(* calculated by adding those weights: *)
CostOfPath[mysecondgraph, {1, 3, 5, 6}]
(* Cost of another path: *)
CostOfPath[mysecondgraph, {1, 2, 5, 6}]
(* Any weight that was not specified is considered to be "one": *)
CostOfPath[mysecondgraph, {1, 2, 5, 6, 4}]
(* ShortestPath find the path with minimum CostOfPath from one initial vertex to a final vertex: *)
ShortestPath[mysecondgraph, 1, 6]
(* By José Luis Gómez-Muñoz
http://homepage.cem.itesm.mx/lgomez/
Mexico *)