-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgraph.cpp
More file actions
120 lines (91 loc) · 2.11 KB
/
Copy pathgraph.cpp
File metadata and controls
120 lines (91 loc) · 2.11 KB
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
116
117
/*
* graph.cpp
* topicNet
*
* Created by basak alper on 5/30/10.
* Copyright 2010 ucsb. All rights reserved.
*
*/
#include "graph.h"
Graph :: Graph()
{
}
Graph :: ~Graph()
{
}
void Graph :: addGraphNode(GraphNode * gn){
adjlist.push_back( gn );
}
void Graph :: addGraphEdge(GraphEdge * ge){
edgelist.push_back( ge );
}
int Graph:: getSize(){
return adjlist.size();
}
GraphEdge* Graph :: getGraphEdge(int index){
GraphEdge* edg;
try {
edg = edgelist.at(index);
if( !edg ) throw "Graph:getGraphEdge edge index empty !";
}
catch( char * str ) {
printf("Graph:getGraphEdge Exception raised: %s \n", str);
}
return edg;
}
GraphNode* Graph ::getGraphNode(int index){
GraphNode* nd;
try {
nd = adjlist.at(index);
if( !nd ) throw "Graph:getGraphNode node index empty !";
}
catch( char * str ) {
printf("Graph:getGraphNode Exception raised: %s \n", str);
}
return nd;
}
void Graph :: preprocess(){
//we want all the edges to have pointer to the nodes its connecting
//we also want all nodes to know about its adjacency nodes
//we will do so by traversing all the edges
/*
for (int e=0; e< edgelist.size(); e++) {
GraphEdge * thedg = edgelist.at(e);
int frid = thedg->from;
int toid = thedg->to;
bool foundfrom = false;
bool foundto = false;
GraphNode * fromend;
GraphNode * toend;
//find these nodes in the adjlist
for (int a=0; a<adjlist.size(); a++) {
GraphNode * nd = adjlist.at(a);
if (!foundfrom && nd->getnodeid() == frid) {
fromend = nd;
thedg->setFrom(fromend);
fromend->addAdjacentEdge(thedg);
foundfrom = true;
}
if (!foundto && nd->getnodeid() == toid) {
toend = nd;
thedg->setTo(toend);
toend->addAdjacentEdge(thedg);
foundto = true;
}
if(foundto && foundfrom){
fromend -> addAdjacentNode(toend);
toend->addAdjacentNode(fromend);
break;
}
}
}
*/
}
vector<GraphNode * > Graph :: firstNghbrs(int nodeind){
vector<GraphNode * > thenodes;
if (nodeind > -1.0 && nodeind < adjlist.size()) {
GraphNode * gn = adjlist.at(nodeind);
thenodes = gn -> nodesvec();
}
return thenodes;
}