Expression of Fortune storing method based class6- subject left in FIG 1

1. Title: Expression of storing map

Storage figure: 1) adjacent table 2) adjacency matrix
how to express chart? Generated map?

2. Analysis

(1) Method adjacency table

① unweighted
Here Insert Picture Description
If both undirected graph 1-> 2, there are 2->. 1.
② with weights
Here Insert Picture Description

(2) the adjacency matrix

Here Insert Picture Description

(3) the expression of matrix (Subject often appear)

There are three vertical columns, each weight respectively weights, sources from, directed to
Here Insert Picture Description

3. FIG stored in memory

In memory, commonly expressed class.

(1)class Graph

Graph class bit and two major sides, using hash_map storage node, using information stored in the edge hash_set

class Graph
{
public:
	unordered_map<int,Node*> nodes;
	unordered_set<Edge*> edges; 
	Graph(){};		
};

Below is to be construed nodes and edges

(2)class Node

Data node has five parts, namely, the value of the node value, the degree of in, out of the OUT, nexts next node of the current node, and the sides starting from the current node.

class Node
{
public:
	int value;
	int	out;
	int in;
	list<Node*> nexts;
	list<Edge*> edges;

	Node(int val,int inn = 0,int outt = 0):value(val),in(inn),out(outt){}
	
};

(3)class Edge

Data node has three parts, namely, the right edge of the weight, and to the current node from the side

class Edge
{
public:
	int weight;
	Node* from;
	Node* to;

	Edge(int wgt,Node* f,Node* t):weight(wgt),from(f),to(t){}
	
};

(4)class GraphGenerator

The package would be a function generating class. Note: The use of (3) in a manner matrix
generating process FIG follows,
① Mr. into a graph object according to the rows in the array, the weights assigned to the first element, a second, a third element represents the value assigned to the node and from to;
② to determine the current figure from, as to whether there is worth the node does not exist, create a node in the graph
③ and then from the node to map out, build edge;
point node to node ④ supplement from, edge out degrees. Supplementary to the degree of the node;
⑤ added to the established edge side in FIG.

class GraphGenerator
{
public:
	Graph createGraph(vector<vector<int> > matrix)
	{
		Graph graph;//需要graph的node和edge 、node有五个data、edge有三个data 
		for(int i = 0; i < matrix.size();++i)
		{
			int weight = matrix[i][0];
			int from = matrix[i][1];
			int to = matrix[i][2];
			//Graph的node里不含有from节点、to节点时建上 
			if(graph.nodes.find(from) == graph.nodes.end())
				graph.nodes[from] = new Node(from);
			if(graph.nodes.find(to) == graph.nodes.end())
				graph.nodes[to] = new Node(to);
			//拿出from,to点
			Node* fromNode = graph.nodes[from];		
			Node* toNode = graph.nodes[to];
			//Node* fromNode = graph.nodes.find(from)->second;		
			//Node* toNode = graph.nodes.find(to)->second;
			//新建边 
			Edge* newEdge = new Edge(weight, fromNode, toNode);
			
			//增加节点的四个数据 
			fromNode->nexts.push_back(toNode);
			fromNode->edges.push_back(newEdge);
			fromNode->out++;
			toNode->in++;
			
			
			graph.edges.insert(newEdge);	
		}
		return graph; 
	} 
};

(5) Testing

All the output nodes of the graph and edges

	//test 
	unordered_map<int,Node*>::iterator ite1 = graph.nodes.begin();
	while(ite1 != graph.nodes.end())
	{
	 	cout << "节点: "<<(ite1)->second->value<<"、";
		//cout << "节点: "<<(ite1)->first<<"、";
	 	ite1++;
	}
	cout<<endl<<"-----------------------------------------------"<<endl ;
	unordered_set<Edge*>::iterator ite = graph.edges.begin();
	while(ite != graph.edges.end())
	{
	 	cout << "边权为 "<<(*ite)->weight<<"    ";
	 	cout<<(*ite)->from->value <<"---->"<<(*ite)->to->value<<endl; 
	 	ite++;
	}
	cout<<endl<<"-----------------------------------------------"<<endl ;

4. The complete code

#include <iostream>

#include<unordered_map> 
#include<unordered_set>
#include<list>
#include<vector> 
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

class Edge;
class Node
{
public:
	int value;
	int	out;
	int in;
	list<Node*> nexts;
	list<Edge*> edges;

	Node(int val,int inn = 0,int outt = 0):value(val),in(inn),out(outt){}	
};

class Edge
{
public:
	int weight;
	Node* from;
	Node* to;

	Edge(int wgt,Node* f,Node* t):weight(wgt),from(f),to(t){}	
};

class Graph
{
public:
	unordered_map<int,Node*> nodes;
	unordered_set<Edge*> edges; 

	Graph(){};		
};

class GraphGenerator
{
public:
	Graph createGraph(vector<vector<int> > matrix)
	{
		Graph graph;//需要graph的node和edge 、node有五个data、edge有三个data 
		for(int i = 0; i < matrix.size();++i)
		{
			int weight = matrix[i][0];
			int from = matrix[i][1];
			int to = matrix[i][2];
			//Graph的node里不含有from节点、to节点时建上 
			if(graph.nodes.find(from) == graph.nodes.end())
				graph.nodes[from] = new Node(from);
			if(graph.nodes.find(to) == graph.nodes.end())
				graph.nodes[to] = new Node(to);
			//拿出from,to点
			Node* fromNode = graph.nodes[from];		
			Node* toNode = graph.nodes[to];
			//Node* fromNode = graph.nodes.find(from)->second;		
			//Node* toNode = graph.nodes.find(to)->second;
			//新建边 
			Edge* newEdge = new Edge(weight, fromNode, toNode);
			
			//增加节点的四个数据 
			fromNode->nexts.push_back(toNode);
			fromNode->edges.push_back(newEdge);
			fromNode->out++;
			toNode->in++;
			
			
			graph.edges.insert(newEdge);	
		}
		return graph; 
	} 
};

int main(int argc, char** argv) {
	GraphGenerator g;
	vector<vector<int> > matrix= {{7,1,2},{5,1,3},{2,2,3}};
	Graph graph = g.createGraph(matrix);

	//test 
	unordered_map<int,Node*>::iterator ite1 = graph.nodes.begin();
	while(ite1 != graph.nodes.end())
	{
	 	cout << "节点: "<<(ite1)->second->value<<"、";
		//cout << "节点: "<<(ite1)->first<<"、";
	 	ite1++;
	}
	cout<<endl<<"-----------------------------------------------"<<endl ;
	unordered_set<Edge*>::iterator ite = graph.edges.begin();
	while(ite != graph.edges.end())
	{
	 	cout << "边权为 "<<(*ite)->weight<<"    ";
	 	cout<<(*ite)->from->value <<"---->"<<(*ite)->to->value<<endl; 
	 	ite++;
	}
	cout<<endl<<"-----------------------------------------------"<<endl ;
	
	return 0;
}

Here Insert Picture Description
Here Insert Picture Description

Published 51 original articles · won praise 1 · views 1358

Guess you like

Origin blog.csdn.net/shi_xiao_xuan/article/details/104860257