Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何禁止起始节点和终点之间的连接呢? #2353

Closed
xutingfeng opened this issue Jul 13, 2022 · 9 comments
Closed

如何禁止起始节点和终点之间的连接呢? #2353

xutingfeng opened this issue Jul 13, 2022 · 9 comments
Labels
type: discussion 讨论 Usage questions, guidance, and other discussions

Comments

@xutingfeng
Copy link

问题描述

我希望起点和终点不能相互连接,现在是可以连一条线

重现链接

0

重现步骤

0

预期行为

0

平台

  • 操作系统: [macOS, Windows, Linux, React Native ...]
  • 网页浏览器: [Google Chrome, Safari, Firefox]
  • X6 版本: [1.28.2 ... ]
    0

屏幕截图或视频(可选)

No response

补充说明(可选)

No response

@x6-bot
Copy link
Contributor

x6-bot bot commented Jul 13, 2022

👋 @xutingfeng

Thanks for opening your first issue here! If you're reporting a 🐞 bug, please make sure you include steps to reproduce it.

To help make it easier for us to investigate your issue, please follow the contributing guidelines.

We get a lot of issues on this repo, so please be patient and we will get back to you as soon as we can.

@tonywu6 tonywu6 added the type: discussion 讨论 Usage questions, guidance, and other discussions label Jul 13, 2022
@tonywu6
Copy link
Contributor

tonywu6 commented Jul 13, 2022

见 Graph 配置 > connecting > allowLoop 配置

const graph = new Graph({
  connecting: {
    allowLoop: false, // forbids self edges
  }
})

@xutingfeng
Copy link
Author

是这样的,
3
我不想让图中的起点和终点连接,改如何做呢,我尝试了allowMulti和allowLoop,都没有达到预期的效果

@babachao
Copy link

我认为可以在你的起点和终端节点中将对应的code值,添加到自定义属性data中,然后在连线时拿到这个自定义的值做判断

@xutingfeng
Copy link
Author

我之前有这么想过,但是项目中是不被允许的,因为不确定哪个节点是起点,哪个节点是终点

@tonywu6
Copy link
Contributor

tonywu6 commented Jul 14, 2022

这里猜测你想要这个图是一个无环图?也就说所有的 Edge 不能形成一个环路?

如果是的话,目前 X6 本身没有开箱即用的支持,但是可以通过为 Graph 配置中的 connecting 配置提供 validateConnection 函数进行判断;

这里有一个之前我使用过的用于这一目的的函数 (仅供参考,对时间/空间复杂度不作考虑)

/**
 * Check if the graph will have (or already has) a cycle if there
 * had been an edge from source to target.
 *
 * @param graph The graph to check.
 * @param source The originating node.
 * @param target The node that is about to receive an incoming edge from source.
 * @returns true if the resulting graph will have node, or false otherwise.
 */
export function willHaveCycle(graph: Graph, source: Node, target: Node): boolean {
  const preorder: Set<Node> = new Set();

  function dfs(origin: Node): boolean {
    if (preorder.has(origin)) return true;
    preorder.add(origin);
    for (const incoming of graph.getIncomingEdges(origin) ?? []) {
      const parent = incoming.getSourceNode();
      if (!parent) continue;
      if (dfs(parent)) return true;
    }
    preorder.delete(origin);
    return false;
  }

  preorder.add(target);
  return dfs(source);
}

也可以考虑 AntV 公共算法库 antvis/algorithmdetectDirectedCycle 函数

关于环路检测的更多细节:https://www.geeksforgeeks.org/detect-cycle-in-a-graph/ https://www.baeldung.com/cs/detecting-cycles-in-directed-graph

@babachao
Copy link

嗦嘎,原来是这样的,那可以看一下楼下tonywu6的逻辑

@NewByVector
Copy link
Contributor

请问上面的回答有解决你的问题吗,为了高效沟通,我们暂时关闭这个 issue,如果有必要,请重新开一个新的 issue。

@x6-bot
Copy link
Contributor

x6-bot bot commented Aug 22, 2023

This thread has been automatically locked because it has not had recent activity.

Please open a new issue for related bugs and link to relevant comments in this thread.

@x6-bot x6-bot bot locked as resolved and limited conversation to collaborators Aug 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: discussion 讨论 Usage questions, guidance, and other discussions
Projects
None yet
Development

No branches or pull requests

4 participants