mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-03-21 12:23:13 +08:00
435 lines
32 KiB
HTML
435 lines
32 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
|
|
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
|
|
<meta name="generator" content="Doxygen 1.9.7"/>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
|
<title>Algorithms_in_C++: CycleCheck Class Reference</title>
|
|
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="../../jquery.js"></script>
|
|
<script type="text/javascript" src="../../dynsections.js"></script>
|
|
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="../../resize.js"></script>
|
|
<script type="text/javascript" src="../../navtreedata.js"></script>
|
|
<script type="text/javascript" src="../../navtree.js"></script>
|
|
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="../../search/searchdata.js"></script>
|
|
<script type="text/javascript" src="../../search/search.js"></script>
|
|
<script type="text/x-mathjax-config">
|
|
MathJax.Hub.Config({
|
|
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
|
|
jax: ["input/TeX","output/HTML-CSS"],
|
|
});
|
|
</script>
|
|
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML/MathJax.js"></script>
|
|
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
|
|
</head>
|
|
<body>
|
|
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
|
|
<div id="titlearea">
|
|
<table cellspacing="0" cellpadding="0">
|
|
<tbody>
|
|
<tr id="projectrow">
|
|
<td id="projectalign">
|
|
<div id="projectname">Algorithms_in_C++<span id="projectnumber"> 1.0.0</span>
|
|
</div>
|
|
<div id="projectbrief">Set of algorithms implemented in C++.</div>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<!-- end header part -->
|
|
<!-- Generated by Doxygen 1.9.7 -->
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
|
|
/* @license-end */
|
|
</script>
|
|
<script type="text/javascript" src="../../menudata.js"></script>
|
|
<script type="text/javascript" src="../../menu.js"></script>
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
$(function() {
|
|
initMenu('../../',true,false,'search.php','Search');
|
|
$(document).ready(function() { init_search(); });
|
|
});
|
|
/* @license-end */
|
|
</script>
|
|
<div id="main-nav"></div>
|
|
</div><!-- top -->
|
|
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
|
<div id="nav-tree">
|
|
<div id="nav-tree-contents">
|
|
<div id="nav-sync" class="sync"></div>
|
|
</div>
|
|
</div>
|
|
<div id="splitbar" style="-moz-user-select:none;"
|
|
class="ui-resizable-handle">
|
|
</div>
|
|
</div>
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
$(document).ready(function(){initNavTree('d3/dbb/class_cycle_check.html','../../'); initResizable(); });
|
|
/* @license-end */
|
|
</script>
|
|
<div id="doc-content">
|
|
<!-- window showing the filter options -->
|
|
<div id="MSearchSelectWindow"
|
|
onmouseover="return searchBox.OnSearchSelectShow()"
|
|
onmouseout="return searchBox.OnSearchSelectHide()"
|
|
onkeydown="return searchBox.OnSearchSelectKey(event)">
|
|
</div>
|
|
|
|
<!-- iframe showing the search results (closed by default) -->
|
|
<div id="MSearchResultsWindow">
|
|
<div id="MSearchResults">
|
|
<div class="SRPage">
|
|
<div id="SRIndex">
|
|
<div id="SRResults"></div>
|
|
<div class="SRStatus" id="Loading">Loading...</div>
|
|
<div class="SRStatus" id="Searching">Searching...</div>
|
|
<div class="SRStatus" id="NoMatches">No Matches</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="header">
|
|
<div class="summary">
|
|
<a href="#pub-static-methods">Static Public Member Functions</a> |
|
|
<a href="#pri-types">Private Types</a> |
|
|
<a href="#pri-static-methods">Static Private Member Functions</a> |
|
|
<a href="../../d8/d55/class_cycle_check-members.html">List of all members</a> </div>
|
|
<div class="headertitle"><div class="title">CycleCheck Class Reference</div></div>
|
|
</div><!--header-->
|
|
<div class="contents">
|
|
<table class="memberdecls">
|
|
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="pub-static-methods" name="pub-static-methods"></a>
|
|
Static Public Member Functions</h2></td></tr>
|
|
<tr class="memitem:ad9a270ffba3a68539b92272c702e3474"><td class="memItemLeft" align="right" valign="top">static bool </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d3/dbb/class_cycle_check.html#ad9a270ffba3a68539b92272c702e3474">isCyclicDFS</a> (<a class="el" href="../../da/d9a/class_graph.html">Graph</a> const &graph)</td></tr>
|
|
<tr class="separator:ad9a270ffba3a68539b92272c702e3474"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a399292a33edf87499daa52b51315aca5"><td class="memItemLeft" align="right" valign="top">static bool </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d3/dbb/class_cycle_check.html#a399292a33edf87499daa52b51315aca5">isCyclicBFS</a> (<a class="el" href="../../da/d9a/class_graph.html">Graph</a> const &graph)</td></tr>
|
|
<tr class="separator:a399292a33edf87499daa52b51315aca5"><td class="memSeparator" colspan="2"> </td></tr>
|
|
</table><table class="memberdecls">
|
|
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="pri-types" name="pri-types"></a>
|
|
Private Types</h2></td></tr>
|
|
<tr class="memitem:ae68a1f08ee8c0b6323ab0f281862eadf"><td class="memItemLeft" align="right" valign="top">enum  </td><td class="memItemRight" valign="bottom"><b>nodeStates</b> : uint8_t { <b>not_visited</b> = 0
|
|
, <b>in_stack</b>
|
|
, <b>visited</b>
|
|
}</td></tr>
|
|
<tr class="separator:ae68a1f08ee8c0b6323ab0f281862eadf"><td class="memSeparator" colspan="2"> </td></tr>
|
|
</table><table class="memberdecls">
|
|
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="pri-static-methods" name="pri-static-methods"></a>
|
|
Static Private Member Functions</h2></td></tr>
|
|
<tr class="memitem:a2f4485c08b45e7a21a2e86f9c3f01d8b"><td class="memItemLeft" align="right" valign="top">static bool </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d3/dbb/class_cycle_check.html#a2f4485c08b45e7a21a2e86f9c3f01d8b">isCyclicDFSHelper</a> (<a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/map.html">AdjList</a> const &adjList, <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>< nodeStates > *state, unsigned int <a class="el" href="../../d5/da1/structnode.html">node</a>)</td></tr>
|
|
<tr class="separator:a2f4485c08b45e7a21a2e86f9c3f01d8b"><td class="memSeparator" colspan="2"> </td></tr>
|
|
</table>
|
|
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
|
|
<div class="textblock"><p>Check if a directed graph has a cycle or not.</p>
|
|
<p>This class provides 2 methods to check for cycle in a directed graph: isCyclicDFS & isCyclicBFS.</p>
|
|
<ul>
|
|
<li>isCyclicDFS uses DFS traversal method to check for cycle in a graph.</li>
|
|
<li>isCyclidBFS used BFS traversal method to check for cycle in a graph. </li>
|
|
</ul>
|
|
</div><h2 class="groupheader">Member Enumeration Documentation</h2>
|
|
<a id="ae68a1f08ee8c0b6323ab0f281862eadf" name="ae68a1f08ee8c0b6323ab0f281862eadf"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#ae68a1f08ee8c0b6323ab0f281862eadf">◆ </a></span>nodeStates</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="mlabels">
|
|
<tr>
|
|
<td class="mlabels-left">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">enum CycleCheck::nodeStates : uint8_t</td>
|
|
</tr>
|
|
</table>
|
|
</td>
|
|
<td class="mlabels-right">
|
|
<span class="mlabels"><span class="mlabel">private</span></span> </td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
<div class="fragment"><div class="line"><span class="lineno"> 160</span>: uint8_t { not_visited = 0, in_stack, visited };</div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<h2 class="groupheader">Member Function Documentation</h2>
|
|
<a id="a399292a33edf87499daa52b51315aca5" name="a399292a33edf87499daa52b51315aca5"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a399292a33edf87499daa52b51315aca5">◆ </a></span>isCyclicBFS()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="mlabels">
|
|
<tr>
|
|
<td class="mlabels-left">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">static bool CycleCheck::isCyclicBFS </td>
|
|
<td>(</td>
|
|
<td class="paramtype"><a class="el" href="../../da/d9a/class_graph.html">Graph</a> const & </td>
|
|
<td class="paramname"><em>graph</em></td><td>)</td>
|
|
<td></td>
|
|
</tr>
|
|
</table>
|
|
</td>
|
|
<td class="mlabels-right">
|
|
<span class="mlabels"><span class="mlabel">inline</span><span class="mlabel">static</span></span> </td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
<p>Check if a graph has cycle or not.</p>
|
|
<p>This function uses BFS to check if a graph is cyclic or not.</p>
|
|
<dl class="params"><dt>Parameters</dt><dd>
|
|
<table class="params">
|
|
<tr><td class="paramname">graph</td><td>which needs to be evaluated for the presence of cycle. </td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
<dl class="section return"><dt>Returns</dt><dd>true if a cycle is detected, else false. </dd></dl>
|
|
<div class="fragment"><div class="line"><span class="lineno"> 249</span> {</div>
|
|
<div class="line"><span class="lineno"> 250</span> <span class="keyword">auto</span> graphAjdList = <a class="code hl_namespace" href="../../df/dce/namespacegraph.html">graph</a>.getAdjList();</div>
|
|
<div class="line"><span class="lineno"> 251</span> <span class="keyword">auto</span> vertices = <a class="code hl_namespace" href="../../df/dce/namespacegraph.html">graph</a>.getVertices();</div>
|
|
<div class="line"><span class="lineno"> 252</span> </div>
|
|
<div class="line"><span class="lineno"> 253</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector<unsigned int></a> indegree(vertices, 0);</div>
|
|
<div class="line"><span class="lineno"> 254</span> <span class="comment">// Calculate the indegree i.e. the number of incident edges to the node.</span></div>
|
|
<div class="line"><span class="lineno"> 255</span> <span class="keywordflow">for</span> (<span class="keyword">auto</span> <span class="keyword">const</span>& <a class="code hl_struct" href="../../d8/d10/structlist.html">list</a> : graphAjdList) {</div>
|
|
<div class="line"><span class="lineno"> 256</span> <span class="keyword">auto</span> children = <a class="code hl_struct" href="../../d8/d10/structlist.html">list</a>.second;</div>
|
|
<div class="line"><span class="lineno"> 257</span> <span class="keywordflow">for</span> (<span class="keyword">auto</span> <span class="keyword">const</span>& child : children) {</div>
|
|
<div class="line"><span class="lineno"> 258</span> indegree[child]++;</div>
|
|
<div class="line"><span class="lineno"> 259</span> }</div>
|
|
<div class="line"><span class="lineno"> 260</span> }</div>
|
|
<div class="line"><span class="lineno"> 261</span> </div>
|
|
<div class="line"><span class="lineno"> 262</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/queue.html">std::queue<unsigned int></a> can_be_solved;</div>
|
|
<div class="line"><span class="lineno"> 263</span> <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code hl_class" href="../../d5/da1/structnode.html">node</a> = 0; <a class="code hl_class" href="../../d5/da1/structnode.html">node</a> < vertices; <a class="code hl_class" href="../../d5/da1/structnode.html">node</a>++) {</div>
|
|
<div class="line"><span class="lineno"> 264</span> <span class="comment">// If a node doesn't have any input edges, then that node will</span></div>
|
|
<div class="line"><span class="lineno"> 265</span> <span class="comment">// definately not result in a cycle and can be visited safely.</span></div>
|
|
<div class="line"><span class="lineno"> 266</span> <span class="keywordflow">if</span> (!indegree[<a class="code hl_class" href="../../d5/da1/structnode.html">node</a>]) {</div>
|
|
<div class="line"><span class="lineno"> 267</span> can_be_solved.<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/queue/emplace.html">emplace</a>(<a class="code hl_class" href="../../d5/da1/structnode.html">node</a>);</div>
|
|
<div class="line"><span class="lineno"> 268</span> }</div>
|
|
<div class="line"><span class="lineno"> 269</span> }</div>
|
|
<div class="line"><span class="lineno"> 270</span> </div>
|
|
<div class="line"><span class="lineno"> 271</span> <span class="comment">// Vertices that need to be traversed.</span></div>
|
|
<div class="line"><span class="lineno"> 272</span> <span class="keyword">auto</span> remain = vertices;</div>
|
|
<div class="line"><span class="lineno"> 273</span> <span class="comment">// While there are safe nodes that we can visit.</span></div>
|
|
<div class="line"><span class="lineno"> 274</span> <span class="keywordflow">while</span> (!can_be_solved.<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/queue/empty.html">empty</a>()) {</div>
|
|
<div class="line"><span class="lineno"> 275</span> <span class="keyword">auto</span> solved = can_be_solved.<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/queue/front.html">front</a>();</div>
|
|
<div class="line"><span class="lineno"> 276</span> <span class="comment">// Visit the node.</span></div>
|
|
<div class="line"><span class="lineno"> 277</span> can_be_solved.<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/queue/pop.html">pop</a>();</div>
|
|
<div class="line"><span class="lineno"> 278</span> <span class="comment">// Decrease number of nodes that need to be traversed.</span></div>
|
|
<div class="line"><span class="lineno"> 279</span> remain--;</div>
|
|
<div class="line"><span class="lineno"> 280</span> </div>
|
|
<div class="line"><span class="lineno"> 281</span> <span class="comment">// Visit all the children of the visited node.</span></div>
|
|
<div class="line"><span class="lineno"> 282</span> <span class="keyword">auto</span> it = graphAjdList.find(solved);</div>
|
|
<div class="line"><span class="lineno"> 283</span> <span class="keywordflow">if</span> (it != graphAjdList.end()) {</div>
|
|
<div class="line"><span class="lineno"> 284</span> <span class="keywordflow">for</span> (<span class="keyword">auto</span> child : it->second) {</div>
|
|
<div class="line"><span class="lineno"> 285</span> <span class="comment">// Check if we can visited the node safely.</span></div>
|
|
<div class="line"><span class="lineno"> 286</span> <span class="keywordflow">if</span> (--indegree[child] == 0) {</div>
|
|
<div class="line"><span class="lineno"> 287</span> <span class="comment">// if node can be visited safely, then add that node to</span></div>
|
|
<div class="line"><span class="lineno"> 288</span> <span class="comment">// the visit queue.</span></div>
|
|
<div class="line"><span class="lineno"> 289</span> can_be_solved.<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/queue/emplace.html">emplace</a>(child);</div>
|
|
<div class="line"><span class="lineno"> 290</span> }</div>
|
|
<div class="line"><span class="lineno"> 291</span> }</div>
|
|
<div class="line"><span class="lineno"> 292</span> }</div>
|
|
<div class="line"><span class="lineno"> 293</span> }</div>
|
|
<div class="line"><span class="lineno"> 294</span> </div>
|
|
<div class="line"><span class="lineno"> 295</span> <span class="comment">// If there are still nodes that we can't visit, then it means that</span></div>
|
|
<div class="line"><span class="lineno"> 296</span> <span class="comment">// there is a cycle and return true, else return false.</span></div>
|
|
<div class="line"><span class="lineno"> 297</span> <span class="keywordflow">return</span> !(remain == 0);</div>
|
|
<div class="line"><span class="lineno"> 298</span> }</div>
|
|
<div class="ttc" id="aemplace_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/container/queue/emplace.html">std::queue::emplace</a></div><div class="ttdeci">T emplace(T... args)</div></div>
|
|
<div class="ttc" id="aempty_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/container/queue/empty.html">std::queue::empty</a></div><div class="ttdeci">T empty(T... args)</div></div>
|
|
<div class="ttc" id="afront_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/container/queue/front.html">std::queue::front</a></div><div class="ttdeci">T front(T... args)</div></div>
|
|
<div class="ttc" id="anamespacegraph_html"><div class="ttname"><a href="../../df/dce/namespacegraph.html">graph</a></div><div class="ttdoc">Graph Algorithms.</div></div>
|
|
<div class="ttc" id="apop_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/container/queue/pop.html">std::queue::pop</a></div><div class="ttdeci">T pop(T... args)</div></div>
|
|
<div class="ttc" id="aqueue_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/container/queue.html">std::queue</a></div></div>
|
|
<div class="ttc" id="astructlist_html"><div class="ttname"><a href="../../d8/d10/structlist.html">list</a></div><div class="ttdef"><b>Definition</b> hash_search.cpp:29</div></div>
|
|
<div class="ttc" id="astructnode_html"><div class="ttname"><a href="../../d5/da1/structnode.html">node</a></div><div class="ttdef"><b>Definition</b> binary_search_tree.cpp:11</div></div>
|
|
<div class="ttc" id="avector_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a></div></div>
|
|
</div><!-- fragment --><div class="dynheader">
|
|
Here is the call graph for this function:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d3/dbb/class_cycle_check_a399292a33edf87499daa52b51315aca5_cgraph.svg" width="372" height="184"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
<a id="ad9a270ffba3a68539b92272c702e3474" name="ad9a270ffba3a68539b92272c702e3474"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#ad9a270ffba3a68539b92272c702e3474">◆ </a></span>isCyclicDFS()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="mlabels">
|
|
<tr>
|
|
<td class="mlabels-left">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">static bool CycleCheck::isCyclicDFS </td>
|
|
<td>(</td>
|
|
<td class="paramtype"><a class="el" href="../../da/d9a/class_graph.html">Graph</a> const & </td>
|
|
<td class="paramname"><em>graph</em></td><td>)</td>
|
|
<td></td>
|
|
</tr>
|
|
</table>
|
|
</td>
|
|
<td class="mlabels-right">
|
|
<span class="mlabels"><span class="mlabel">inline</span><span class="mlabel">static</span></span> </td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
<p>Driver function to check if a graph has a cycle.</p>
|
|
<p>This function uses DFS to check for cycle in the graph.</p>
|
|
<dl class="params"><dt>Parameters</dt><dd>
|
|
<table class="params">
|
|
<tr><td class="paramname">graph</td><td>which needs to be evaluated for the presence of cycle. </td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
<dl class="section return"><dt>Returns</dt><dd>true if a cycle is detected, else false. </dd></dl>
|
|
<p>State of the node.</p>
|
|
<p>It is a vector of "nodeStates" which represents the state node is in. It can take only 3 values: "not_visited", "in_stack", and "visited".</p>
|
|
<p>Initially, all nodes are in "not_visited" state.</p>
|
|
<div class="fragment"><div class="line"><span class="lineno"> 212</span> {</div>
|
|
<div class="line"><span class="lineno"> 213</span> <span class="keyword">auto</span> vertices = <a class="code hl_namespace" href="../../df/dce/namespacegraph.html">graph</a>.getVertices();</div>
|
|
<div class="line"><span class="lineno"> 214</span><span class="comment"></span> </div>
|
|
<div class="line"><span class="lineno"> 215</span><span class="comment"> /** State of the node.</span></div>
|
|
<div class="line"><span class="lineno"> 216</span><span class="comment"> *</span></div>
|
|
<div class="line"><span class="lineno"> 217</span><span class="comment"> * It is a vector of "nodeStates" which represents the state node is in.</span></div>
|
|
<div class="line"><span class="lineno"> 218</span><span class="comment"> * It can take only 3 values: "not_visited", "in_stack", and "visited".</span></div>
|
|
<div class="line"><span class="lineno"> 219</span><span class="comment"> *</span></div>
|
|
<div class="line"><span class="lineno"> 220</span><span class="comment"> * Initially, all nodes are in "not_visited" state.</span></div>
|
|
<div class="line"><span class="lineno"> 221</span><span class="comment"> */</span></div>
|
|
<div class="line"><span class="lineno"> 222</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector<nodeStates></a> state(vertices, not_visited);</div>
|
|
<div class="line"><span class="lineno"> 223</span> </div>
|
|
<div class="line"><span class="lineno"> 224</span> <span class="comment">// Start visiting each node.</span></div>
|
|
<div class="line"><span class="lineno"> 225</span> <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code hl_class" href="../../d5/da1/structnode.html">node</a> = 0; <a class="code hl_class" href="../../d5/da1/structnode.html">node</a> < vertices; <a class="code hl_class" href="../../d5/da1/structnode.html">node</a>++) {</div>
|
|
<div class="line"><span class="lineno"> 226</span> <span class="comment">// If a node is not visited, only then check for presence of cycle.</span></div>
|
|
<div class="line"><span class="lineno"> 227</span> <span class="comment">// There is no need to check for presence of cycle for a visited</span></div>
|
|
<div class="line"><span class="lineno"> 228</span> <span class="comment">// node as it has already been checked for presence of cycle.</span></div>
|
|
<div class="line"><span class="lineno"> 229</span> <span class="keywordflow">if</span> (state[<a class="code hl_class" href="../../d5/da1/structnode.html">node</a>] == not_visited) {</div>
|
|
<div class="line"><span class="lineno"> 230</span> <span class="comment">// Check for cycle.</span></div>
|
|
<div class="line"><span class="lineno"> 231</span> <span class="keywordflow">if</span> (<a class="code hl_function" href="../../d3/dbb/class_cycle_check.html#a2f4485c08b45e7a21a2e86f9c3f01d8b">isCyclicDFSHelper</a>(<a class="code hl_namespace" href="../../df/dce/namespacegraph.html">graph</a>.getAdjList(), &state, <a class="code hl_class" href="../../d5/da1/structnode.html">node</a>)) {</div>
|
|
<div class="line"><span class="lineno"> 232</span> <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
|
|
<div class="line"><span class="lineno"> 233</span> }</div>
|
|
<div class="line"><span class="lineno"> 234</span> }</div>
|
|
<div class="line"><span class="lineno"> 235</span> }</div>
|
|
<div class="line"><span class="lineno"> 236</span> </div>
|
|
<div class="line"><span class="lineno"> 237</span> <span class="comment">// All nodes have been safely traversed, that means there is no cycle in</span></div>
|
|
<div class="line"><span class="lineno"> 238</span> <span class="comment">// the graph. Return false.</span></div>
|
|
<div class="line"><span class="lineno"> 239</span> <span class="keywordflow">return</span> <span class="keyword">false</span>;</div>
|
|
<div class="line"><span class="lineno"> 240</span> }</div>
|
|
<div class="ttc" id="aclass_cycle_check_html_a2f4485c08b45e7a21a2e86f9c3f01d8b"><div class="ttname"><a href="../../d3/dbb/class_cycle_check.html#a2f4485c08b45e7a21a2e86f9c3f01d8b">CycleCheck::isCyclicDFSHelper</a></div><div class="ttdeci">static bool isCyclicDFSHelper(AdjList const &adjList, std::vector< nodeStates > *state, unsigned int node)</div><div class="ttdef"><b>Definition</b> cycle_check_directed_graph.cpp:170</div></div>
|
|
</div><!-- fragment --><div class="dynheader">
|
|
Here is the call graph for this function:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d3/dbb/class_cycle_check_ad9a270ffba3a68539b92272c702e3474_cgraph.svg" width="598" height="86"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
<a id="a2f4485c08b45e7a21a2e86f9c3f01d8b" name="a2f4485c08b45e7a21a2e86f9c3f01d8b"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a2f4485c08b45e7a21a2e86f9c3f01d8b">◆ </a></span>isCyclicDFSHelper()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="mlabels">
|
|
<tr>
|
|
<td class="mlabels-left">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">static bool CycleCheck::isCyclicDFSHelper </td>
|
|
<td>(</td>
|
|
<td class="paramtype"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/map.html">AdjList</a> const & </td>
|
|
<td class="paramname"><em>adjList</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>< nodeStates > * </td>
|
|
<td class="paramname"><em>state</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">unsigned int </td>
|
|
<td class="paramname"><em>node</em> </td>
|
|
</tr>
|
|
<tr>
|
|
<td></td>
|
|
<td>)</td>
|
|
<td></td><td></td>
|
|
</tr>
|
|
</table>
|
|
</td>
|
|
<td class="mlabels-right">
|
|
<span class="mlabels"><span class="mlabel">inline</span><span class="mlabel">static</span><span class="mlabel">private</span></span> </td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
<p>Helper function of "isCyclicDFS".</p>
|
|
<dl class="params"><dt>Parameters</dt><dd>
|
|
<table class="params">
|
|
<tr><td class="paramname">adjList</td><td>is the adjacency list representation of some graph. </td></tr>
|
|
<tr><td class="paramname">state</td><td>is the state of the nodes of the graph. </td></tr>
|
|
<tr><td class="paramname">node</td><td>is the node being evaluated.</td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
<dl class="section return"><dt>Returns</dt><dd>true if graph has a cycle, else false. </dd></dl>
|
|
<div class="fragment"><div class="line"><span class="lineno"> 172</span> {</div>
|
|
<div class="line"><span class="lineno"> 173</span> <span class="comment">// Add node "in_stack" state.</span></div>
|
|
<div class="line"><span class="lineno"> 174</span> (*state)[<a class="code hl_class" href="../../d5/da1/structnode.html">node</a>] = in_stack;</div>
|
|
<div class="line"><span class="lineno"> 175</span> </div>
|
|
<div class="line"><span class="lineno"> 176</span> <span class="comment">// If the node has children, then recursively visit all children of the</span></div>
|
|
<div class="line"><span class="lineno"> 177</span> <span class="comment">// node.</span></div>
|
|
<div class="line"><span class="lineno"> 178</span> <span class="keyword">auto</span> <span class="keyword">const</span> it = adjList.find(<a class="code hl_class" href="../../d5/da1/structnode.html">node</a>);</div>
|
|
<div class="line"><span class="lineno"> 179</span> <span class="keywordflow">if</span> (it != adjList.end()) {</div>
|
|
<div class="line"><span class="lineno"> 180</span> <span class="keywordflow">for</span> (<span class="keyword">auto</span> child : it->second) {</div>
|
|
<div class="line"><span class="lineno"> 181</span> <span class="comment">// If state of child node is "not_visited", evaluate that child</span></div>
|
|
<div class="line"><span class="lineno"> 182</span> <span class="comment">// for presence of cycle.</span></div>
|
|
<div class="line"><span class="lineno"> 183</span> <span class="keyword">auto</span> state_of_child = (*state)[child];</div>
|
|
<div class="line"><span class="lineno"> 184</span> <span class="keywordflow">if</span> (state_of_child == not_visited) {</div>
|
|
<div class="line"><span class="lineno"> 185</span> <span class="keywordflow">if</span> (<a class="code hl_function" href="../../d3/dbb/class_cycle_check.html#a2f4485c08b45e7a21a2e86f9c3f01d8b">isCyclicDFSHelper</a>(adjList, state, child)) {</div>
|
|
<div class="line"><span class="lineno"> 186</span> <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
|
|
<div class="line"><span class="lineno"> 187</span> }</div>
|
|
<div class="line"><span class="lineno"> 188</span> } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (state_of_child == in_stack) {</div>
|
|
<div class="line"><span class="lineno"> 189</span> <span class="comment">// If child node was "in_stack", then that means that there</span></div>
|
|
<div class="line"><span class="lineno"> 190</span> <span class="comment">// is a cycle in the graph. Return true for presence of the</span></div>
|
|
<div class="line"><span class="lineno"> 191</span> <span class="comment">// cycle.</span></div>
|
|
<div class="line"><span class="lineno"> 192</span> <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
|
|
<div class="line"><span class="lineno"> 193</span> }</div>
|
|
<div class="line"><span class="lineno"> 194</span> }</div>
|
|
<div class="line"><span class="lineno"> 195</span> }</div>
|
|
<div class="line"><span class="lineno"> 196</span> </div>
|
|
<div class="line"><span class="lineno"> 197</span> <span class="comment">// Current node has been evaluated for the presence of cycle and had no</span></div>
|
|
<div class="line"><span class="lineno"> 198</span> <span class="comment">// cycle. Mark current node as "visited".</span></div>
|
|
<div class="line"><span class="lineno"> 199</span> (*state)[<a class="code hl_class" href="../../d5/da1/structnode.html">node</a>] = visited;</div>
|
|
<div class="line"><span class="lineno"> 200</span> <span class="comment">// Return that current node didn't result in any cycles.</span></div>
|
|
<div class="line"><span class="lineno"> 201</span> <span class="keywordflow">return</span> <span class="keyword">false</span>;</div>
|
|
<div class="line"><span class="lineno"> 202</span> }</div>
|
|
</div><!-- fragment --><div class="dynheader">
|
|
Here is the call graph for this function:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d3/dbb/class_cycle_check_a2f4485c08b45e7a21a2e86f9c3f01d8b_cgraph.svg" width="374" height="86"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
<hr/>The documentation for this class was generated from the following file:<ul>
|
|
<li>graph/cycle_check_directed_graph.cpp</li>
|
|
</ul>
|
|
</div><!-- contents -->
|
|
</div><!-- doc-content -->
|
|
<!-- start footer part -->
|
|
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
|
|
<ul>
|
|
<li class="navelem"><a class="el" href="../../d3/dbb/class_cycle_check.html">CycleCheck</a></li>
|
|
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.7 </li>
|
|
</ul>
|
|
</div>
|
|
</body>
|
|
</html>
|