- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有这个 1...n 的图,现在我找到了一个拓扑排序的解决方案,我怎么知道它是否是一个有效的解决方案?例如,对于 n = 5;和这种连接1->2,2->3,1->3,1->5,
我有一个解决方案 4->1->5->2->3 。它有效吗?可能是我对topsort的想法不清楚。
为了方便起见,这是我的代码
int incoming[n],A[n][n];
priority_queue<int> Q;
while(m--){
int i,j;
cin>>i>>j;
A[i][j] = 1;
++incoming[j];
}
for(int i=1;i<=n;++i)
if(!incoming[i])
Q.push(i);
vector<int> toplist;
while(!Q.empty()){
int u = Q.top(); Q.pop();
toplist.push_back(u);
for(int i = 1;i<=n;++i)
{
if(A[u][i])
{
A[u][i] = 0;
--incoming[i];
if(!incoming[i])
Q.push(i);
}
}
}
for(int i=0;i<toplist.size();++i)
cout<<toplist[i]<<" ";
最佳答案
我在做拓扑排序时遇到了完全相同的问题。 有一个名为 bValidateTopSortResult() 的函数可以验证结果。 如果从 U 到 V 存在一条边,则 U 必须在最高排序中排在 V 之前。 重要的是要跟踪所有相邻的顶点。我已经存储在一个列表中。
#include <iostream>
#include <list>
#include <stack>
#include <map>
using namespace std;
struct GRAPH{
int iVertices;
list<int> *ptrAdj; //Has structure like ptrAdj[i]={j,k,l}. This means that
//i is from-vertex and j,k,l are to-vertices.
};
int iTopSortOrder[10];
map<int, int> mapTopSort;
//
//Recursive function.
//Key is iterating list<>*ptrAdj.
//
void
vTopSortRecursive( GRAPH* pG, int iVertexID, bool *bVisited, stack<int>& stResult )
{
bVisited[iVertexID] = true;
//Goes thru all adjacent vertices.
for( auto it : pG->ptrAdj[iVertexID]){
if(bVisited[it] == false)
vTopSortRecursive( pG, it, bVisited, stResult );
}
//Pushes the vertex ID after all its children adjacent vertices are already
pushed.
stResult.push(iVertexID);
}
void
vTopSort( GRAPH* ptrGraph )
{
stack<int> stResult;
bool bVisited[ptrGraph->iVertices];
int iTopSortIndex = 0;
for(int i=0; i < ptrGraph->iVertices; ++i )
bVisited[i] = false;
for(int i=0; i < ptrGraph->iVertices; ++i )
if(bVisited[i] == false )
vTopSortRecursive( ptrGraph, i, bVisited, stResult );
cout << "Top sort result" << endl;
while( !stResult.empty() ){
int iTop = stResult.top();
cout << iTop << endl;
//Keeps track of the order of the vertex top sort.
//mapTopSort[i] = j means vertex ID 'i' is in jth order in array.
mapTopSort[iTop] = iTopSortIndex;
//Keeps the top sort order in an array.
iTopSortOrder[iTopSortIndex] = stResult.top();;
stResult.pop();
iTopSortIndex++;
}
}
//All parent ID must come befoe children ID in top sort.
//mapTopSort[i] = j; means that vertex ID 'i' is in jth place in the order.
bool
bValidateTopSortResult( GRAPH* ptrGraph )
{
bool bValid=true;
for(int iVertexID=0; iVertexID<ptrGraph->iVertices;iVertexID++)
{
for(auto it: ptrGraph->ptrAdj[iVertexID] ){
if( mapTopSort[iVertexID] > mapTopSort[it] ){
bValid = false;
cout << "Invalid sort" << endl;
cout << iVertexID << " must come before " << mapTopSort[it] << endl;
}
}
}
return bValid;
}
int
main(void)
{
GRAPH *ptrGraph = new GRAPH;
ptrGraph->iVertices = 6;
ptrGraph->ptrAdj = new list<int>[6];
ptrGraph->ptrAdj[5].push_back(2);
ptrGraph->ptrAdj[5].push_back(0);
ptrGraph->ptrAdj[4].push_back(0);
ptrGraph->ptrAdj[4].push_back(1);
ptrGraph->ptrAdj[2].push_back(3);
ptrGraph->ptrAdj[3].push_back(1);
vTopSort( ptrGraph );
if (bValidateTopSortResult( ptrGraph ))
cout << "Sort is valid";
else
cout <<"Sort is INVALID";
return 0;
}
关于c++ - 我怎么知道拓扑排序是否有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19337187/
是否可以可视化kubernetes拓扑并看到它在添加/删除/链接对象时实时更新? 我在https://www.youtube.com/watch?v=38SNQPhsGBk上观看了一个视频,其中服务/
我在搜索 Rack 拓扑时发现了这个问题...可能是hadoop认证问题: 您的群集在三个不同的 Rack 中都有从属节点,并且您编写了一个 Rack 拓扑脚本,以将每台计算机分别识别为位于 Rack
我拿了sticky force layout示例并尝试添加额外链接并使用 enter() 更新布局,但随后所有链接都消失了,FireBug 也没有显示任何错误。 这些行不应该添加链接吗? graph.
我需要编写一个订单管理器,将客户(股票、外汇等)订单发送到适当的交易所。客户想要发送订单,但对 FIX 或其他专有协议(protocol)一无所知,只知道发送订单的内部(规范化)格式。我有应用程序(服
我正在尝试使用 Eclipse 在远程主机上提交 Storm 拓扑。 这是我的代码: Config conf = new Config(); conf.setDebug(false); conf.se
这是一个名为 mininet 的流行网络模拟器的拓扑文件 我创建了一个 MultiSwitch() 类,我想将其传递给我的 Topology 类以用作默认开关 有没有办法做到这一点?我对Python不
我从 cat/proc/cpuinfo 中了解到,我正在使用 Intel(R) Core(TM) i5 CPU M 560 @ 2.67GHz。但是我想知道确切的层次结构,比如有多少个套接字,每个套接
我正在学习storm。我对我们可以在 Apache Storm 上一次运行的拓扑数量有疑问。我已经在 Storm 集群上提交了两个拓扑,但一次只运行了一个拓扑。我需要杀死或停用已经存在的拓扑拓扑以运行
我正在尝试理解 topology of queues并交换 MT 在 RabbitMQ 中创建的。 我不能得到这两个陈述: we generate an exchange for each queue
我正在寻找一种方法来测试 Kafka Streams 应用程序。这样我就可以定义输入事件,测试套件会向我显示输出。 如果没有真正的 Kafka 设置,这可能吗? 最佳答案 更新 Kafka 1.1.0
我正在使用 Java 类将拓扑提交到 Storm 集群,我还计划使用 Java 类来终止拓扑。但根据 Storm documentation ,以下命令用于终止拓扑并且没有 Java 方法(这是有正当
Storm jar storm-starter-topologies-0.10.0-beta1.jar storm-starter-master.jar生产拓扑本地 我遇到了错误: Running:
我正在编写一个 dockerized Java Spring 应用程序,该应用程序使用 Apache Storm v1.1.2、Kafka v0.11.0.1、Zookeeper 3.4.6、Eure
我在 jts 拓扑库中有一些多边形。如果我想在 javafx Pane 上绘图,我会这样做: Polygon poly=new Polygon();//javafx //g is geometry
我需要在 Java GUI 应用程序中动态绘制(星形)拓扑。通过星形拓扑,我的意思是这样的: (来源:thebryantadvantage.com) 不需要太花哨,但我不想做得太丑陋和粗糙。我所说的动
我想自动化设置 Mininet 的过程虚拟机,通过 SSH 连接到 VM,在 VM 中启动 Mininet,并初始化拓扑。我需要 session 保持打开状态,以便我可以使用创建的网络向 Minine
我正在尝试重新平衡使用 KafkaSpout 的 Storm 拓扑。我的代码是: TopologyBuilder builder = new TopologyBuilder(); Pr
标题几乎说明了一切,我有一些 Storm 拓扑,我想测量它们的延迟,即来自 Kafka 的消息与最终相关执行的最后一点之间的时间量 bolt 。如果我可以深入研究结果以查看每个 bolt 之间的延迟,
假设我想让一些转换“A”可配置。此转换使用状态存储管理某些状态,并且还需要重新分区,这意味着仅在配置后才会进行重新分区。现在,如果我按照以下方式(或任何其他组合)运行应用程序 3 次(也可能是滚动升级
我目前正在尝试实现与 R 语言集成的 Storm 拓扑。 作为起点,我采用了以下项目 ( https://github.com/allenday/R-Storm ),它通过扩展 ShellBolt 类
我是一名优秀的程序员,十分优秀!