Created
May 31, 2012 20:05
-
-
Save itsjohncs/2845876 to your computer and use it in GitHub Desktop.
Graph Lab 9
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <map> | |
#include <set> | |
#include <iostream> | |
#include <string> // Don't know if neccessary... | |
#include <stdexcept> // Provides the RuntimeError class | |
using namespace std; | |
class Graph { | |
typedef string NodeName; | |
typedef set<NodeName> AdjacencyRow; | |
typedef map<NodeName, AdjacencyRow> AdjacencyList; | |
AdjacencyList adjacency_list; | |
public: | |
bool node_exists(NodeName zname) | |
{ | |
return adjacency_list.count(zname) != 0; | |
} | |
void add_node(NodeName zname) | |
{ | |
if (node_exists(zname)) | |
throw runtime_error("Bad! No Duplicates Allowed!"); | |
adjacency_list[zname]; | |
} | |
void add_relationship(NodeName zfirst, NodeName zsecond) | |
{ | |
if (!(node_exists(zfirst) && node_exists(zsecond))) | |
throw runtime_error("Both nodes must exist!"); | |
adjacency_list[zfirst].insert(zsecond); | |
adjacency_list[zsecond].insert(zfirst); | |
} | |
void print(ostream & zout = cout) | |
{ | |
for (AdjacencyList::iterator i = adjacency_list.begin(); | |
i != adjacency_list.end(); ++i) { | |
zout << i->first << ": "; | |
for (AdjacencyRow::iterator j = i->second.begin(); | |
j != i->second.end(); ++j) { | |
zout << *j << " "; | |
} | |
zout << "\n"; | |
} | |
} | |
}; | |
int main() { | |
Graph graph; | |
graph.add_node("A"); | |
graph.add_node("B"); | |
graph.add_node("C"); | |
graph.add_relationship("A", "B"); | |
graph.add_relationship("A", "C"); | |
graph.add_relationship("B", "C"); | |
graph.print(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment