HOG2
FloydWarshall.cpp
Go to the documentation of this file.
1 /*
2  * FloydWarshall.cpp
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 12/7/08.
6  * Copyright 2008 __MyCompanyName__. All rights reserved.
7  *
8  */
9 
10 #include "FloydWarshall.h"
11 
12 //1 /* Assume a function edgeCost(i,j) which returns the cost of the edge from i to j
13 // 2 (infinity if there is none).
14 // 3 Also assume that n is the number of vertices and edgeCost(i,i)=0
15 // 4 */
16 //5
17 //6 int path[][];
18 //7 /* A 2-dimensional matrix. At each step in the algorithm, path[i][j] is the shortest path
19 // 8 from i to j using intermediate vertices (1..k-1). Each path[i][j] is initialized to
20 // 9 edgeCost(i,j).
21 // 10 */
22 //11
23 void FloydWarshall(Graph *g, std::vector<std::vector<double> > &lengths)
24 {
25  lengths.resize(0);
26  lengths.resize(g->GetNumNodes());
27  for (int x = 0; x < g->GetNumNodes(); x++)
28  {
29  lengths[x].resize(g->GetNumNodes());
30  for (int i = 0; i < g->GetNumNodes(); i++)
31  lengths[x][i] = 1e10;
32  }
33 
34  for (int x = 0; x < g->GetNumEdges(); x++)
35  {
36  edge *e = g->GetEdge(x);
37  lengths[e->getFrom()][e->getTo()] = e->GetWeight();
38  lengths[e->getTo()][e->getFrom()] = e->GetWeight();
39  }
40 
41  for (int x = 0; x < g->GetNumNodes(); x++)
42  {
43  for (int i = 0; i < g->GetNumNodes(); i++)
44  {
45  for (int j = 0; j < g->GetNumNodes(); j++)
46  {
47  lengths[i][j] = std::min(lengths[i][j], lengths[i][x]+lengths[x][j]);
48  }
49  }
50  }
51 }
52 
edge::GetWeight
double GetWeight()
Definition: Graph.h:140
edge::getFrom
unsigned int getFrom() const
Definition: Graph.h:146
edge::getTo
unsigned int getTo() const
Definition: Graph.h:147
min
double min(double a, double b)
Definition: FPUtil.h:35
Graph
A generic Graph class.
Definition: Graph.h:66
Graph::GetNumEdges
int GetNumEdges()
Definition: Graph.cpp:397
Graph::GetNumNodes
int GetNumNodes()
Definition: Graph.cpp:403
FloydWarshall
void FloydWarshall(Graph *g, std::vector< std::vector< double > > &lengths)
Definition: FloydWarshall.cpp:23
FloydWarshall.h
Graph::GetEdge
edge * GetEdge(unsigned long num)
Definition: Graph.cpp:164
edge
Edge class for connections between node in a Graph.
Definition: Graph.h:129