#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; class PageRank { public: map Nodes; /* This helps to read in nodes */ vector Names; /* Nodes are numbers, so this is the name of each node. */ vector < vector > Adj; /* The Adjacency List */ int Get_Node(const string &name); /* Return a node's number. Create it if it doesn't exist. */ void Read(); /* Create the graph from standard input. */ }; /* If we've seen this name before, return the number of its node. Otherwise, add the node to Nodes, the name to Names, and make room in the Adjacency List */ int PageRank::Get_Node(const string &name) { int number; if (Nodes.find(name) != Nodes.end()) return Nodes[name]; number = Adj.size(); Nodes[name] = number; Adj.resize(number+1); Names.push_back(name); return number; } /* Read the graph from standard input */ void PageRank::Read() { string line, from, to; int separator; int f, t; while (getline(cin, line)) { if (line.size() == 0 || line[0] != '#') { /* Ignore comments */ separator = line.find(" -> "); if (separator != string::npos) { /* Ignore lines without -> " */ from = line.substr(0, separator); to = line.substr(separator + 4); f = Get_Node(from); /* Get the node numbers, and put t onto */ t = Get_Node(to); /* f's adjacency list. */ Adj[f].push_back(t); } } } } int main() { PageRank PR; int i, j; PR.Read(); /* Print out the nodes and edges. */ for (i = 0; i < PR.Adj.size(); i++) { printf("Node %s:\n", PR.Names[i].c_str()); for (j = 0; j < PR.Adj[i].size(); j++) { printf(" Edge to: %s\n", PR.Names[PR.Adj[i][j]].c_str()); } } exit(0); }