summaryrefslogtreecommitdiff
path: root/cpp/src/IceStorm/WeightedGraph.h
blob: fc5b5b2c39a574dde737f04a4e6e8dcf5b9e4ab0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// **********************************************************************
//
// Copyright (c) 2003-2007 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#ifndef WEIGHTED_GRAPH_H
#define WEIGHTED_GRAPH_H

#include <IceUtil/Config.h>
#include <vector>
#include <list>
#include <map>

namespace IceStorm
{

//
// Forward declaration.
//
class SAXGraphHandler;

//
// A weighted graph. This class uses internally a vertex, adjacency
// matrix representation. Each vertex has a name. The [i,j] of the
// adjacency graph represents the the edge from _vertices[i] to
// _vertices[j].
//
class WeightedGraph
{
public:

    WeightedGraph(bool = false);
    ~WeightedGraph();

    //
    // Parse the graph from the given XML file. Returns false in event
    // of a parse error.
    //
    bool parse(const std::string&);

    //
    // Compute a new adjacency matrix with the given cost.
    //
    void compute(std::vector<int>&, int);

    //
    // Render the edge table in ASCII format.
    //
    void dump(std::ostream&);

    //
    // Swap the adjacency matrix. This could be actual swap.
    //
    void swap(const std::vector<int>&);

    //
    // Get all the vertices.
    //
    std::vector<std::string> getVertices() const;

    //
    // Get the set of edges for a vertex. The return data is the name
    // of the second vertex, and the cost.
    //
    std::vector<std::pair<std::string, int> > getEdgesFor(const std::string&) const;

private:

    //
    // Recursive function used in computation of new edge set.
    //
    void visit(unsigned int, int, std::list<std::pair<unsigned int, int> >&, int);

    //
    // Callbacks from parsing routines
    //
    void addVertex(const std::string&);
    void addEdge(const std::string&, const std::string&, int);

    void error();

    //
    // List of vertices.
    //
    std::vector<std::string> _vertices;

    //
    // Lookup table - maps from vertex name to vertex index.
    //
    std::map<std::string, int> _vlookup;

    //
    // The adjacency matrix. -1 indicates no edge, otherwise the value
    // is the cost.
    //
    std::vector<int> _edges;

    //
    // Is the graph reflective? That is if there is edge (v1, v2,
    // cost) then there is (v2, v1, cost).
    //
    bool _reflective;

    int _error;

    friend class GraphHandler;
};

} // End namespace IceStorm

#endif