- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我需要帮助来设计图形的最佳数据结构。以下是我的实现。请就此设计发表您的看法。
#ifndef _GRAPH_H_
#define _GRAPH_H_
#include <map>
#include <vector>
#include <iostream>
#include <list>
template <typename T>
class Vertex {
public:
Vertex(){};
Vertex(T inVertex): m_vertex(inVertex), m_visited(false){}
~Vertex(){}
bool operator<(const Vertex<T>& right) const { return m_vertex < right.m_vertex;}
T getVertex () { return m_vertex;}
T getVisited () { return m_visited;}
T getParent () { return m_vertexParentVisited;}
void setVisited(bool inVisited) { m_visited = inVisited;}
void setParent(T inParentVertex) { m_vertexParentVisited = inParentVertex;}
private:
T m_vertex;
T m_vertexParentVisited;
bool m_visited;
};
template <typename T>
class Edge
{
public:
enum EDGE_TYPE
{
TREE_EDGE,
PARENT_EDGE,
BACK_EDGE,
DOWN_EDGE
};
Edge(Vertex<T>* inSrc, Vertex<T>* inDst)
{
m_SourceVertex = inSrc;
m_DestVertex = inDst;
}
void SetEdgeType( EDGE_TYPE t)
{
m_EdgeType = t;
}
private:
Vertex<T>* m_SourceVertex;
Vertex<T>* m_DestVertex;
EDGE_TYPE m_EdgeType;
protected:
};
template <typename T>
class Graph
{
public:
typedef Vertex<T> GraphVertex;
// Adjancency List Datastructure and Iterator declaration.
// Use STL List here
typedef std::list<GraphVertex*> AdjList;
typedef typename AdjList::iterator AdjListIterator;
// Graph Data structure declaration and Iterator declaration
// Graph is map of GraphVertex* and List of adjacency list.
typedef std::map<GraphVertex*, AdjList> GraphMap;
typedef typename GraphMap::iterator GraphIterator;
// Graph Memory pool is map where actual memory allocation
// will happen.
// Make sure you have some way to identify each vertex.
// Map key is the identification method of the vertex.
// Map value is the pointer to actual object.
typedef std::map<T,GraphVertex*> GraphMemoryPool;
typedef typename GraphMemoryPool::iterator GraphInMemoryIterator;
// Edge Class Declaration.
typedef std::vector<Edge<T> > GraphEdge;
private:
bool m_isDirected;
GraphMap m_graph;
GraphMemoryPool m_graphMemoryPool;
std::vector<T> m_SearchOrderVector;
GraphEdge m_GraphEdge;
public:
Graph(bool inIsDirected):m_isDirected(inIsDirected) {}
~Graph()
{
for ( GraphInMemoryIterator itr = m_graphMemoryPool.begin(); itr != m_graphMemoryPool.end(); ++itr)
{
if ( itr->second) delete itr->second;
}
m_graphMemoryPool.clear();
m_graph.clear();
}
void insert(T inSRC, T inDST)
{
if ( m_isDirected)
{
__insert(inSRC,inDST);
}
else
{
__insert(inSRC,inDST);
__insert(inDST,inSRC);
}
}
void printGraph()
{
for ( GraphIterator itr = m_graph.begin(); itr != m_graph.end(); ++itr)
{
std::cout << static_cast<GraphVertex*>(itr->first)->getVertex() << " : ";
AdjList tmp = static_cast<AdjList>(itr->second);
for ( AdjListIterator itr1 = tmp.begin(); itr1 != tmp.end(); ++itr1)
{
std::cout << (*itr1)->getVertex() << " ";
}
std::cout << std::endl;
}
}
void printSearchOrder()
{
std::vector<T> tmp = getSearchOrderVector();
for ( vector<T>::iterator itr = tmp.begin(); itr != tmp.end(); ++itr)
{
std::cout << *itr << " ";
}
std::cout << std::endl;
}
void printParentLinkMap()
{
std::vector<T> tmp = getSearchOrderVector();
for ( vector<T>::iterator itr = tmp.begin(); itr != tmp.end(); ++itr)
{
GraphVertex *tmp = __VertexInstance(*itr);
std::cout << "[" << tmp->getParent() << "] Parent Of [" << tmp->getVertex() << "]" << std::endl;
}
}
void DFS()
{
for ( GraphInMemoryIterator itr = m_graphMemoryPool.begin(); itr != m_graphMemoryPool.end(); ++itr)
{
(*itr).second->setVisited(false);
}
m_SearchOrderVector.clear();
// This loop handles the case when the grpah is not
// Connected.
for ( GraphIterator itr = m_graph.begin(); itr != m_graph.end(); ++itr)
{
GraphVertex *currentVertex = static_cast<GraphVertex*>(itr->first);
if ( currentVertex->getVisited() == false)
{
T curVertex = currentVertex->getVertex();
// Insert new element in Search Order Vector.
m_SearchOrderVector.push_back(curVertex);
// Set the parent of this root node in the DFS search tree
currentVertex->setParent(curVertex);
// Create Edge with itself here.
__setTypeAndInsertNewEdge( curVertex,
curVertex,
Edge<T>::TREE_EDGE);
// Mark the vertex as visited.
currentVertex->setVisited(true);
__rundfs(itr);
}
}
}
std::vector<T> getSearchOrderVector() { return m_SearchOrderVector;}
int getNumberOfVertex(){ return (int)m_graph.size();}
private:
void __setTypeAndInsertNewEdge( T inSRC, T inDST, typename Edge<T>::EDGE_TYPE t )
{
Edge<T> tmp(__VertexInstance(inSRC),
__VertexInstance(inDST));
tmp.SetEdgeType(t);
m_GraphEdge.push_back(tmp);
}
// Recursive DFS function.
// Apart from visiting vertices
// this implementatioin will be doing following.
// 1. Maintain the order in which vertices are visited.
// 2. Update Parent link map
// 3. Create Edge and update the type of the edge.
void __rundfs( typename GraphMap::iterator &itr)
{
for (AdjListIterator itr1 = itr->second.begin(); itr1 != itr->second.end(); ++itr1)
{
GraphVertex *childVertex = (*itr1);
GraphVertex *parentVertex = itr->first;
if ( childVertex->getVisited() == false)
{
m_SearchOrderVector.push_back(childVertex->getVertex()); // Update the search order Vector.
childVertex->setParent(parentVertex->getVertex()); // Update the parent-link map.
childVertex->setVisited(true); // Mark the vertex as visited.
__setTypeAndInsertNewEdge(parentVertex->getVertex(),
childVertex->getVertex(),
Edge<T>::TREE_EDGE); // setup the edge type
__rundfs(m_graph.find(
__VertexInstance(childVertex->getVertex())));
}
else
{
if ( childVertex->getVertex() == parentVertex->getVertex())
{
}
}
}
}
void __insert(T inSRC, T inDST)
{
GraphIterator itr = m_graph.find(__VertexInstance(inSRC));
if ( itr != m_graph.end())
{
// Update the Adjancency list
itr->second.push_back(__VertexInstance(inDST));
}
else
{
// Create new Adjancy list
m_graph.insert(std::make_pair(__VertexInstance(inSRC),AdjList() ));
GraphIterator itr = m_graph.find(__VertexInstance(inSRC));
// Update the Adjacency List
itr->second.push_back(__VertexInstance(inDST));
}
}
// Searches if the Vertex is already allocated in the pool map
// If ye return the pointer from the map.
// Else Create new Vertex instance
// Insert into the memory pool map
// Return the pointer.
GraphVertex *__VertexInstance(T inVertex)
{
GraphVertex *newVertex = (GraphVertex *)0;
GraphInMemoryIterator itr = m_graphMemoryPool.find(inVertex);
if ( itr == m_graphMemoryPool.end())
{
newVertex = new GraphVertex(inVertex);
m_graphMemoryPool.insert(std::make_pair(inVertex,newVertex));
}
else
{
newVertex = ( GraphVertex *)itr->second;
}
return newVertex;
}
};
#endif
最佳答案
看来你知道这一点,但我正在审查以使所有人受益:
有两种典型的图实现方式:边矩阵和稀疏矩阵(表示为每个顶点的边 vector )。
矩阵是一个 NxN 结构,其中 N 是顶点数。每个数据是两个顶点之间的边长,因此 G[1][2] 是从 1 到 2 的距离。这也允许有向图。
另一种更适合稀疏图的方法是为每个顶点设置一个边 vector 。所以你会得到 vector> g,其中 g[1] 是顶点 1 的边 vector 。该 vector 中的每个项目都是 edge,其中包含它到达的顶点和距离。
有关更多详细信息,请访问此处:http://en.wikipedia.org/wiki/Graph_(data_structure)#Representations
鉴于所有这些,其中一种实现可能对您非常有用。
编辑1
为了在图中存储权重,on 可能会使用诸如 vector> 之类的结构(如上所述)。在这种情况下,边缘类可能是这样的:
class edge {
int weight;
int destination_vertex;
};
这样,从1到2的边就是g[1][2]。
关于c++ - 在 C++ 中实现图形的想法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3853101/
我需要将文本放在 中在一个 Div 中,在另一个 Div 中,在另一个 Div 中。所以这是它的样子: #document Change PIN
奇怪的事情发生了。 我有一个基本的 html 代码。 html,头部, body 。(因为我收到了一些反对票,这里是完整的代码) 这是我的CSS: html { backgroun
我正在尝试将 Assets 中的一组图像加载到 UICollectionview 中存在的 ImageView 中,但每当我运行应用程序时它都会显示错误。而且也没有显示图像。 我在ViewDidLoa
我需要根据带参数的 perl 脚本的输出更改一些环境变量。在 tcsh 中,我可以使用别名命令来评估 perl 脚本的输出。 tcsh: alias setsdk 'eval `/localhome/
我使用 Windows 身份验证创建了一个新的 Blazor(服务器端)应用程序,并使用 IIS Express 运行它。它将显示一条消息“Hello Domain\User!”来自右上方的以下 Ra
这是我的方法 void login(Event event);我想知道 Kotlin 中应该如何 最佳答案 在 Kotlin 中通配符运算符是 * 。它指示编译器它是未知的,但一旦知道,就不会有其他类
看下面的代码 for story in book if story.title.length < 140 - var story
我正在尝试用 C 语言学习字符串处理。我写了一个程序,它存储了一些音乐轨道,并帮助用户检查他/她想到的歌曲是否存在于存储的轨道中。这是通过要求用户输入一串字符来完成的。然后程序使用 strstr()
我正在学习 sscanf 并遇到如下格式字符串: sscanf("%[^:]:%[^*=]%*[*=]%n",a,b,&c); 我理解 %[^:] 部分意味着扫描直到遇到 ':' 并将其分配给 a。:
def char_check(x,y): if (str(x) in y or x.find(y) > -1) or (str(y) in x or y.find(x) > -1):
我有一种情况,我想将文本文件中的现有行包含到一个新 block 中。 line 1 line 2 line in block line 3 line 4 应该变成 line 1 line 2 line
我有一个新项目,我正在尝试设置 Django 调试工具栏。首先,我尝试了快速设置,它只涉及将 'debug_toolbar' 添加到我的已安装应用程序列表中。有了这个,当我转到我的根 URL 时,调试
在 Matlab 中,如果我有一个函数 f,例如签名是 f(a,b,c),我可以创建一个只有一个变量 b 的函数,它将使用固定的 a=a1 和 c=c1 调用 f: g = @(b) f(a1, b,
我不明白为什么 ForEach 中的元素之间有多余的垂直间距在 VStack 里面在 ScrollView 里面使用 GeometryReader 时渲染自定义水平分隔线。 Scrol
我想知道,是否有关于何时使用 session 和 cookie 的指南或最佳实践? 什么应该和什么不应该存储在其中?谢谢! 最佳答案 这些文档很好地了解了 session cookie 的安全问题以及
我在 scipy/numpy 中有一个 Nx3 矩阵,我想用它制作一个 3 维条形图,其中 X 轴和 Y 轴由矩阵的第一列和第二列的值、高度确定每个条形的 是矩阵中的第三列,条形的数量由 N 确定。
假设我用两种不同的方式初始化信号量 sem_init(&randomsem,0,1) sem_init(&randomsem,0,0) 现在, sem_wait(&randomsem) 在这两种情况下
我怀疑该值如何存储在“WORD”中,因为 PStr 包含实际输出。? 既然Pstr中存储的是小写到大写的字母,那么在printf中如何将其给出为“WORD”。有人可以吗?解释一下? #include
我有一个 3x3 数组: var my_array = [[0,1,2], [3,4,5], [6,7,8]]; 并想获得它的第一个 2
我意识到您可以使用如下方式轻松检查焦点: var hasFocus = true; $(window).blur(function(){ hasFocus = false; }); $(win
我是一名优秀的程序员,十分优秀!