{"id":2931,"date":"2025-04-18T12:37:25","date_gmt":"2025-04-18T12:37:25","guid":{"rendered":"https:\/\/mindfusion.eu\/blog\/?p=2931"},"modified":"2025-04-18T12:49:35","modified_gmt":"2025-04-18T12:49:35","slug":"prevent-cycles-in-the-diagram","status":"publish","type":"post","link":"https:\/\/mindfusion.dev\/blog\/prevent-cycles-in-the-diagram\/","title":{"rendered":"Prevent cycles in the diagram"},"content":{"rendered":"\n<p>A frequent requirement for many kinds of diagrams is to keep their underlying graphs acyclic. If your application lets users draw diagrams interactively, you could prevent forming a cycle by rejecting new link between a pair of nodes that are already connected by a path.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>Say there is a path node1 -> node2 -> \u2026 -> nodeN; letting user draw a link from nodeN to node1 would result in a cycle. We can prevent that by handling the LinkCreating validation event and looking for path using PathFinder class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>diagram.addDiagramListener(new DiagramAdapter()\n{\n    @Override\n    public void linkCreating(LinkValidationEvent e)\n    {\n        PathFinder pathFinder = new PathFinder(diagram);\n        Path path = pathFinder.findShortestPath(\n            e.getDestination(), e.getOrigin());\n        e.setCancel(path != null);\n    }\n});\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2025\/04\/prevent_cycle.png\"><img loading=\"lazy\" decoding=\"async\" width=\"682\" height=\"479\" src=\"https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2025\/04\/prevent_cycle.png\" alt=\"\" class=\"wp-image-2932\" srcset=\"https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2025\/04\/prevent_cycle.png 682w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2025\/04\/prevent_cycle-300x211.png 300w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2025\/04\/prevent_cycle-427x300.png 427w\" sizes=\"auto, (max-width: 682px) 100vw, 682px\" \/><\/a><\/figure>\n\n\n\n<p>Note that if you allow users modify links, you&#8217;d have to apply that logic from LinkModifying event as well.<\/p>\n\n\n\n<p>By default, the control also allows drawing self-loop links. Disable them by setting diagram&#8217;s AllowSelfLoops property to false:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>diagram.setAllowSelfLoops(false);<\/code><\/pre>\n\n\n\n<p>Code above demonstrates MindFusion&#8217;s Java diagramming API, but same events and types exist in our .NET and JavaScript libraries.<\/p>\n\n\n\n<p>Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A frequent requirement for many kinds of diagrams is to keep their underlying graphs acyclic. If your application lets users draw diagrams interactively, you could prevent forming a cycle by rejecting new link between a pair of nodes that are &hellip; <a href=\"https:\/\/mindfusion.dev\/blog\/prevent-cycles-in-the-diagram\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[95,74],"tags":[3],"class_list":["post-2931","post","type-post","status-publish","format-standard","hentry","category-diagramming-2","category-sample-code","tag-diagram"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3RlKs-Lh","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/2931","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/comments?post=2931"}],"version-history":[{"count":5,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/2931\/revisions"}],"predecessor-version":[{"id":2937,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/2931\/revisions\/2937"}],"wp:attachment":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/media?parent=2931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/categories?post=2931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/tags?post=2931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}