Peter Schiff criticized Bitcoin investors for ignoring its declining value against gold.
Bitcoin’s year-to-date performance surpassed gold, rising 47% compared to gold’s 29% gain.
TON has broken out of a symmetrical triangle formation, signaling a potential bullish phase ahead.
The increase in active addresses shows a surge in buying activity, increasing the likelihood o
Data from CoinGecko shows that the price of Bitcoin climbed above $65,000 on Friday for the first time since the beginning of August and now hovers at $65,778.
Traders are searching for crypto with the potential to explode during the highly anticipated 2025 bull market. This article will examine which of Solana (SOL) and RCO Finance (RCOF) has the best chance of rising by 10,000% in 2025. New Crypto Phone Could Boost Solana’s Chances, But By How Much? SOL has been one of […]
Just recently, Spot Bitcoin ETFs saw a significant jump in net positive flows, causing a ripple effect within the cryptocurrency ETF industry. Amidst this, a Bitcoin ETF token, ETFSwap (ETFS, is positioned to take full advantage of this surge, benefitting up to $500 million in liquidity and creating trading opportunities for all types of traders, […]
In computer science, algorithms are the backbone of various useful technologies (like AI), helping us efficiently tackle a wide range of real-life challenges. Among these, graph algorithms hold a special place due to their applicability as many problems can be modeled using graphs. A graph is a collection of nodes (or vertices) connected by edges, representing relationships (or connections) between nodes.
Some examples:
Social Networks: In a social network, like Facebook or Instagram, users can be represented as nodes, and friendships or connections between them are edges. [1]
Route Planning and Navigation: In maps, locations (cities, intersections, or landmarks) are modeled as nodes, and roads or paths between them are edges. [2]
Web Crawling: The web can be seen as a massive graph where web pages are nodes, and hyperlinks between them are edges.
Scheduling and Task Management: Many task dependencies can be modeled as directed acyclic graphs (DAGs), where tasks are nodes and dependencies between them are edges.
Network Connectivity: In computer networks, devices (routers, computers) are nodes, and communication links are edges.
One of the fundamental algorithms used to traverse or explore graphs is Depth First Search (DFS). It is used to explore (visit) all nodes in the graph. Another, complementary and fundamental algorithm to accomplish that goal is called Breadth First Search (BFS) which will be a subject of a separate article.
Here, we will dive into how DFS works, with simple code, intuitive examples, and some cool animations showing you how this algorithm works step-by-step.
DFS can be implemented in two ways: iterative and recursive. Here, I’ll show you how to do it recursively as IMHO it is easier to understand and to code. This is also a fantastic opportunity to learn how recursion works if you’re not familiar with it yet. DFS implementation will be in pure Python.
Below there is a code for the DFS algorithm itself.
There are three inputs to the function: a set of visited nodes (usually initially empty), a graph definition and a starting node. The logic is simple, yet effective:
1. First, we check if we have visited a given node already
a. If yes, skip checking its neighbors
b. If no, print the node and start visiting its neighbors (the “for loop”)
2. Repeat, till all nodes are in the list of visited nodes
In this case, the function returns None (effectively nothing) because it prints the visited nodes and writes them to the set defined externally. We can change its behavior to return a set of all visited nodes without printing values like that:
Example 1
First, we must define our exemplary graph. For this, we’ll use the adjacency matrix as a Python dictionary. In each key-value pair, a key is a node, and a value is a list of nodes connected to it (neighbors).
Below is the code creating the first exemplary graph in the computer memory. In this case, it is a directed graph (for clarity and simplicity) but DFS works well for undirected ones too.
After running a function call command the output is a series of nodes that were visited:
image by author
Or with the alternative version of the code like below. Here we can just make a small change to the input not to use any global variable and pass an empty set directly. Output then is:
image by author
Let’s visualize how a functions stack and a final set is being built step-by-step. This is depicted on the animation below.
image by author
Example 2
In this example, we will build and traverse a special kind of graph — a decision tree. A definition of the graph is below.
After running the DFS on this graph the output is:
image by author
The animation below shows what the graph looks like and how DFS traversed it.
DFS traversing a tree; image by author
Summary
Depth First Search is an essential algorithm in graph theory, widely used across multiple domains from social networks to decision trees. Its recursive nature makes it easy to understand and implement, as demonstrated by the examples in this article. The simplicity of DFS, along with its ability to efficiently explore all nodes in a graph, makes it a powerful tool for solving various computational problems. Understanding how DFS works lays the groundwork for mastering other algorithms such as Breadth First Search (BFS) and path-finding algorithms like Dijkstra’s or A*.
Try experimenting with larger and more complex graphs, and explore how it behaves with different data structures. In future articles, we will explore other traversal methods like BFS and further investigate their use cases, advantages, and limitations.
Keep practicing and pushing your limits, and soon graph algorithms like DFS will become second nature. Happy coding!
References
[1] Tsok, Samuel & Yakubu, Hosea & Solomon, Rwat. (2023). Graph Models of Social Media Network As Applied to Facebook and Facebook Messenger Groups. International Journal on Computer Science and Engineering. Vol. 9. Pg 1. 10.56201/ijcsmt.v9.no1.2023.pg1.12. [link]
[2] Tianlun Dai, Wenchao Zheng, Jiayue Sun, Cun Ji, Tao Zhou, Mingtong Li, Wei Hu, Ziqiang Yu, Continuous Route Planning over a Dynamic Graph in Real-Time, Procedia Computer Science, Volume 174, 2020 [link]
Dice survey shows that tech professionals bruised by a sour job market remain optimistic, but they aren’t completely sold on the use of generative AI in the workplace.
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie
Duration
Description
cookielawinfo-checkbox-analytics
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional
11 months
The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy
11 months
The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.