-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_map.cpp
More file actions
96 lines (80 loc) · 1.68 KB
/
graph_map.cpp
File metadata and controls
96 lines (80 loc) · 1.68 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
//
// main.cpp
// cppstl
//
// Created by jeyong on 27/07/2018.
// Copyright © 2018 jeyong. All rights reserved.
//
#include <iostream>
#include <vector>
#include <map>
using namespace std;
class Vertex {
public:
string Name;
bool operator< (const Vertex& obj) const
{
if(obj.Name < this->Name)
return true;
return false;
}
};
class Neighbor {
public:
string Name;
int cost;
};
class Graph {
public:
Graph(){
}
void AddVertex(Vertex x);
void AddEdge(Vertex sour, Vertex dest, int weight);
void Init();
void Display();
map<Vertex, vector<Neighbor>> vertexs;
};
void Graph::AddVertex(Vertex v)
{
for (auto it = begin(vertexs); it!=end(vertexs); it++)
{
if (it->first.Name == v.Name )
{
return ;
}
}
vertexs.insert(pair<Vertex, vector<Neighbor>>(v, vector<Neighbor>()));
}
void Graph::AddEdge(Vertex sour, Vertex dest, int weight)
{
Neighbor n = {dest.Name, weight};
auto it = vertexs.find(sour);
if (it != vertexs.end()){
it->second.push_back(n);
}
}
void Graph::Display()
{
for (auto it=begin(vertexs); it != end(vertexs); it++)
{
cout<<"Vertex : "<<it->first.Name<<" Neighbors : ";
for (auto e : it->second)
{
cout<<e.Name << " weight: "<<e.cost<<" ";
}
cout<<endl;
}
}
int main(int argc, const char * argv[])
{
Graph g;
Vertex v1, v2;
v1.Name = "A";
v2.Name = "B";
g.AddVertex(v1);
g.AddVertex(v2);
g.AddEdge(v1, v2, 3);
g.AddEdge(v2, v1, 2);
g.Display();
//g.AddEdge(<#Vertex sour#>, <#Vertex dest#>, <#int weight#>)
}