00001 #ifndef __GRAPHADJMAT_H__
00002 #define __GRAPHADJMAP_H__
00003
00004 #pragma once
00005
00006 #include <iostream>
00007 #include <fstream>
00008 #include <vector>
00009 #include <stack>
00010 #include <cstring>
00011
00012 class GraphAdjMat {
00013
00014 private:
00015 unsigned int n;
00016
00017 bool directed;
00018
00019 int ** edges;
00020
00021 int ** detour;
00022
00023 public:
00024
00025 static const int NONE = -1;
00026
00027 typedef std::vector< std::pair< std::pair<int, int>, int> > Path;
00028
00034 inline GraphAdjMat(unsigned int n, bool directed = true) :
00035 n(n),
00036 directed(directed)
00037 {
00038 edges = new int*[n];
00039 detour = new int*[n];
00040 for (unsigned int i = 0; i < n; i++) {
00041 edges[i] = new int[n];
00042 detour[i] = new int[n];
00043 for (unsigned int j = 0; j < n; j++) {
00044 edges[i][j] = NONE;
00045 detour[i][j] = NONE;
00046 }
00047 }
00048 }
00049
00053 inline void clear_paths(){
00054 for (unsigned int i = 0; i < n; i++) {
00055 for (unsigned int j = 0; j < n; j++) {
00056 detour[i][j] = NONE;
00057 }
00058 }
00059 }
00060
00065 inline unsigned int get_n() const {
00066 return n;
00067 }
00068
00073 inline int get_edge(int a, int b) const {
00074 return edges[a][b];
00075 }
00076
00080 inline void set_edge(int a, int b, int cost) {
00081 edges[a][b] = cost;
00082 if (directed == false) {
00083 edges[b][a] = cost;
00084 }
00085 }
00086
00090 inline void erase_edge(int a, int b) {
00091 set_edge(a,b, NONE);
00092 }
00093
00097 inline void set_detour(int a, int b, int det) {
00098 detour[a][b] = det;
00099 if (directed == false) {
00100 detour[b][a] = det;
00101 }
00102 }
00103
00108 inline Path path(int a, int b) const {
00109 Path returnValue;
00110 std::stack< std::pair<int,int> > track;
00111 track.push(std::pair<int,int>(a,b));
00112 while (!track.empty()){
00113 a = track.top().first;
00114 b = track.top().second;
00115 track.pop();
00116 if (detour[a][b] == NONE){
00117 if (edges[a][b] == NONE){
00118 std::cerr << "Broken path! No edge from " << a << " to " << b << "!" << std::endl;
00119 returnValue.clear();
00120 break;
00121 } else {
00122 returnValue.push_back(std::pair< std::pair<int,int>, int>(std::pair<int,int>(a,b), edges[a][b]));
00123 }
00124 } else {
00125 track.push(std::pair<int,int>(detour[a][b], b));
00126 track.push(std::pair<int,int>(a, detour[a][b]));
00127 }
00128 }
00129 return returnValue;
00130 }
00131
00132 friend std::ostream& operator<< (std::ostream& out, Path & path);
00133 friend std::ostream& operator<< (std::ostream& out, GraphAdjMat & graph);
00134 friend std::istream& operator>> (std::istream& in, GraphAdjMat & graph);
00135
00136 ~GraphAdjMat() {
00137 for (unsigned int i = 0; i < n; i++) {
00138 delete[] edges[i];
00139 delete[] detour[i];
00140 }
00141 delete[] edges;
00142 delete[] detour;
00143 }
00144 };
00145
00151 std::ostream& operator<< (std::ostream& out, const GraphAdjMat::Path & path);
00152
00158 std::ostream& operator<< (std::ostream& out, const GraphAdjMat & graph);
00159
00165 std::istream& operator>> (std::istream& out, const GraphAdjMat & graph);
00166
00167 #endif
00168