<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Graph Theory Archives - Be on the Right Side of Change</title>
	<atom:link href="https://blog.finxter.com/tag/graph-theory/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.finxter.com/tag/graph-theory/</link>
	<description></description>
	<lastBuildDate>Fri, 08 Apr 2022 18:41:19 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://blog.finxter.com/wp-content/uploads/2020/08/cropped-cropped-finxter_nobackground-32x32.png</url>
	<title>Graph Theory Archives - Be on the Right Side of Change</title>
	<link>https://blog.finxter.com/tag/graph-theory/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>A Recursive Pathfinder Algorithm in Python</title>
		<link>https://blog.finxter.com/recursive-pathfinder-algorithm-python/</link>
		
		<dc:creator><![CDATA[Chris]]></dc:creator>
		<pubDate>Mon, 15 Feb 2021 22:10:00 +0000</pubDate>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Graph Theory]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Adjacency Matrix]]></category>
		<category><![CDATA[Graph]]></category>
		<category><![CDATA[Path]]></category>
		<category><![CDATA[Pathfinder]]></category>
		<category><![CDATA[Python 3]]></category>
		<category><![CDATA[Recursion]]></category>
		<category><![CDATA[Recursive]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=491</guid>

					<description><![CDATA[<p>A simple and effective way to grow your computer science skills is to master the basics. Knowing the basics sets apart the great coders from the merely intermediate ones. One such basic area in computer science is graph theory, a specific subproblem of which&#8212;the pathfinder algorithm&#8212;we&#8217;ll address in this tutorial. So first things first: What ... <a title="A Recursive Pathfinder Algorithm in Python" class="read-more" href="https://blog.finxter.com/recursive-pathfinder-algorithm-python/" aria-label="Read more about A Recursive Pathfinder Algorithm in Python">Read more</a></p>
<p>The post <a href="https://blog.finxter.com/recursive-pathfinder-algorithm-python/">A Recursive Pathfinder Algorithm in Python</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>A simple and effective way to grow your computer science skills is to master the basics. Knowing the basics sets apart the great coders from the merely intermediate ones. </p>



<p>One such basic area in computer science is <strong><em><a href="https://blog.finxter.com/graph-applications/" title="What are the Applications of Graphs in Computer Science?" target="_blank" rel="noreferrer noopener">graph theory</a></em></strong>, a specific subproblem of which&#8212;the pathfinder algorithm&#8212;we&#8217;ll address in this tutorial. So first things first: </p>



<h2 class="wp-block-heading">What Is a Graph?</h2>



<p>You may already know data structures like <a href="https://blog.finxter.com/python-lists/" title="The Ultimate Guide to Python Lists" target="_blank" rel="noreferrer noopener">lists</a>, <a href="https://blog.finxter.com/sets-in-python/" title="The Ultimate Guide to Python Sets – with Harry Potter Examples" target="_blank" rel="noreferrer noopener">sets</a>, and <a href="https://blog.finxter.com/python-dictionary/" title="Python Dictionary – The Ultimate Guide" target="_blank" rel="noreferrer noopener">dictionaries</a>. These data structures are denoted as complex data structures&#8211;not because they&#8217;re difficult to understand but because they build upon other data structures.</p>



<p>A graph is just another complex data structure for relational data.</p>



<p>Relational data consists of edges and vertices. Each vertex stands in one or more relations with other vertices. </p>



<p>An example of relational data is the Facebook social graph. Facebook represents users as vertices and friendship relations as edges. Two users are connected via an edge in the graph if they are (Facebook) friends.</p>



<p class="has-cyan-bluish-gray-background-color has-background"><strong>What is a graph?</strong> A graph is a basic data structure in computer science. It models relationships between data items. Using graphs to model real-world phenomena is not a new idea. In 1736, Leonhard Euler has invented the graph data structure to solve the problem of <a href="https://en.wikipedia.org/wiki/Seven_Bridges_of_K%C3%B6nigsberg">“seven bridges of Königsberg”</a>. Graphs existed way before the first computer was even an idea. In fact, as we will see in this article, graphs helped to make the computer possible. Without graphs, there wouldn’t be a computer as we now know it today.</p>



<h2 class="wp-block-heading">How to Represent a Graph Data Structure in the Code? </h2>



<p>In this tutorial, we&#8217;ll use an <a rel="noreferrer noopener" title="https://en.wikipedia.org/wiki/Adjacency_matrix" href="https://en.wikipedia.org/wiki/Adjacency_matrix" target="_blank">adjacency matrix</a> as a graph data structure <code>G</code>. </p>



<p>Each row <code>i</code> in the matrix stores the out-neighbors of vertex <code>i</code>. And each column <code>j</code> stores the in-neighbors of vertex <code>j</code>. </p>



<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Thus, there is an edge from vertex <code>i</code> to vertex <code>j</code>, if <code>G[i][j]==1</code>.</p>



<p>You can see an example of the adjacency matrix graph representation in the following code of the Pathfinder algorithm:</p>



<h2 class="wp-block-heading">The Pathfinder Algorithm in Python</h2>



<p>How to determine whether there is a path between two vertices? </p>



<p>The function <code>find_path(graph, v_start, v_end, path_len)</code> checks whether there is a direct or indirect path between two vertices <code>v_start</code> and <code>v_end</code> in graph. We know that there is a direct path between <code>v_start</code> and <code>v_end</code> if both are already neighbors, i.e., <code>graph[v_start][v_end]==1</code>.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def find_path(graph, v_start, v_end, path_len=0):
    '''Is there a path between vertex v_start and vertex v_end?'''

    # Traverse each vertex only once
    if path_len >= len(graph):
        return False

    # Direct path from v_start to v_end?
    if graph[v_start][v_end]:
        return True

    # Indirect path via neighbor v_nbor?
    for v_nbor, edge in enumerate(graph[v_start]):
        if edge:
            # between v_start and v_nbor
            if find_path(graph, v_nbor, v_end, path_len + 1):
                return True

    # No direct or indirect path found
    return False

# The graph represented as adjancy matrix
G = [[1, 1, 0, 0, 0],
     [0, 1, 0, 0, 0],
     [0, 0, 1, 0, 0],
     [0, 1, 1, 1, 0],
     [1, 0, 0, 1, 1]]

print(find_path(graph=G, v_start=3, v_end=0))
# False

print(find_path(G, 3, 1))
# True
</pre>



<p>However, even if there is not a direct path, there could be an indirect path between vertices <code>v_start</code> and <code>v_end</code>. </p>



<p>To check this, the algorithm uses a recursive approach. Specifically, there is an indirect path if a vertex <code>v_nbor</code> exists such that there is a path:</p>



<pre class="wp-block-preformatted"><code> v_start --> v_nbor --> ... --> v_end</code></pre>



<p>The variable <code>path_len</code> stores the length of the current path. </p>



<p>We increment it in each recursion level as the current path length increases by one. Note that all paths with length <code>>=n</code> consist of at least <code>n</code> vertices. </p>



<p>In other words, at least one vertex is visited twice and a cycle exists in this recursion instance. Hence, we skip recursion for paths with lengths greater than or equal to the number of vertices in the graph.</p>



<p>In the code snippet, we check whether there is a path between 3 and 0. </p>



<p>If you understand what the code is doing, it suffices to look at the adjacency matrix <code>G</code>. </p>



<p>There is a direct path from vertex 3 to vertices 1 and 2 (and to itself). But neither vertex 1 nor 2 has any out-neighbors. </p>



<p>Therefore, there is no path from vertex 3 to any other vertex (besides vertices 1 and 2).</p>



<h2 class="wp-block-heading">Related Video</h2>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Graph Theory Overview" width="937" height="527" src="https://www.youtube.com/embed/82zlRaRUsaY?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>






<h2 class="wp-block-heading">Academy Course &#8211; Mastering the Top 10 Graph Algorithms</h2>



<p>If you want to improve your fundamental computer science skills, there&#8217;s nothing more effective than <strong>studying algorithms</strong>. </p>



<p>To help you master the <strong>most important graph algorithms</strong>, we&#8217;ve just launched the &#8220;Top 10 Algorithms&#8221; course at the <a rel="noreferrer noopener" href="https://academy.finxter.com/university/graph-algorithms-in-python/" data-type="URL" data-id="https://academy.finxter.com/university/graph-algorithms-in-python/" target="_blank">Finxter Computer Science Academy</a>. This great course from Finxter Star Creator Matija <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2b50.png" alt="⭐" class="wp-smiley" style="height: 1em; max-height: 1em;" /> teaches you the most important graph algorithms such as BFS, DFS, A*, and Dijkstra. </p>



<p>Understanding these algorithms will not only make you a better coder, but it&#8217;ll also lay a strong foundation on which you can build your whole career as a computer scientist.</p>



<p>Click the screenshot to find out more:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><a href="https://academy.finxter.com/university/graph-algorithms-in-python/" target="_blank" rel="noopener"><img fetchpriority="high" decoding="async" width="363" height="650" src="https://blog.finxter.com/wp-content/uploads/2022/03/image-141.png" alt="" class="wp-image-246565" srcset="https://blog.finxter.com/wp-content/uploads/2022/03/image-141.png 363w, https://blog.finxter.com/wp-content/uploads/2022/03/image-141-168x300.png 168w" sizes="(max-width: 363px) 100vw, 363px" /></a></figure></div>



<div class="wp-block-buttons alignwide is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex">
<div class="wp-block-button has-custom-width wp-block-button__width-50"><a class="wp-block-button__link" href="https://academy.finxter.com/university/graph-algorithms-in-python/" target="_blank" rel="noreferrer noopener">Learn More</a></div>
</div>



<p></p>
<p>The post <a href="https://blog.finxter.com/recursive-pathfinder-algorithm-python/">A Recursive Pathfinder Algorithm in Python</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Minified using Disk

Served from: blog.finxter.com @ 2026-04-07 03:11:54 by W3 Total Cache
-->