Advertisement
Guest User

Untitled

a guest
Dec 20th, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.90 KB | None | 0 0
  1. #ifndef AdjList_H_
  2. #define AdjList_H_
  3.  
  4. #include <iostream>
  5. #include <cstdlib>
  6. #include <string>
  7. #include <vector>
  8. #include <algorithm>
  9. #include <string>
  10. using namespace std;
  11.  
  12. class Edge;
  13.  
  14. struct Vertex
  15. {
  16. public:
  17.     string id;
  18.     vector<Edge> adj;
  19.     Vertex(string name){
  20.         this->id = name;
  21.     }
  22.     ~Vertex(){};
  23.     void addEdge(Vertex* n, int weight){
  24.         Edge edge(n, weight);
  25.         adj.push_back(edge);
  26.     }
  27. };
  28.  
  29. class Edge{
  30. public:
  31.     Edge(Vertex* next, int weight);
  32.     ~Edge();
  33. private:
  34.     Vertex* next; //next node
  35.     int weight;
  36. };
  37.  
  38. Edge::Edge(Vertex* next, int weight){
  39.     next = next;
  40.     weight = weight;
  41. }
  42.  
  43. Edge::~Edge(){}
  44.  
  45. class Graph{
  46. public:
  47.     Graph();
  48.     ~Graph();
  49.     const int returnEdges();
  50.     void addName(string name);
  51.     void fillGraph(char* filename);
  52.  
  53.     int editDist(const string &s1, const string & s2);
  54.  
  55.  
  56. private:
  57.     vector <Vertex*> vertices;
  58. };
  59.  
  60. Graph::Graph(){
  61. }
  62.  
  63. const int Graph::getNumOfNodes(){
  64.     return vertices.size();
  65. }
  66.  
  67. const int Graph::returnEdges(){
  68.     return edges.size();
  69. }
  70. Graph::~Graph()
  71. {
  72. }
  73.  
  74. void Graph::addName(string name){
  75.     Vertex newVertex(name);
  76.     vertices.push_back(newVertex);
  77.  
  78. }
  79.  
  80. void Graph::fillGraph(char* filename){
  81.     ifstream openFile;
  82.     string input = "";
  83.     vector <string> names;
  84.     openFile.open(filename);
  85.  
  86.     while (getline(openFile, input)){
  87.         names.push_back(input);
  88.     } openFile.close();
  89.  
  90.  
  91. }
  92.  
  93.  
  94. //Uses levenshtein's distance to calculate the edit distance
  95. int Graph::editDist(const string &s1, const string & s2) {
  96.     const std::size_t len1 = s1.size(), len2 = s2.size();
  97.  
  98.     vector<int> col(len2 + 1), prevCol(len2 + 1);
  99.  
  100.     for (int i = 0; i < prevCol.size(); i++)
  101.         prevCol[i] = i;
  102.     for (int i = 0; i < len1; i++) {
  103.         col[0] = i + 1;
  104.         for (int j = 0; j < len2; j++)
  105.             col[j + 1] = std::min(std::min(prevCol[1 + j] + 1, col[j] + 1),
  106.             prevCol[j] + (s1[i] == s2[j] ? 0 : 1));
  107.         col.swap(prevCol);
  108.     }
  109.     return prevCol[len2];
  110. }
  111.  
  112.  
  113. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement