Discover more from Algorithmically Speaking
Algorithmically Speaking - #3: Nodes, Edges, and Connectivity
A visual introduction to the basic concepts of graph theory.
Today we will be diving into one of the most common ways of representing data and modeling problems (and solutions) in Computer Science. We will be talking about Graphs.
This will be the first of a series of posts introducing graph theory in a very visual way.
It will consist of some basic definitions of graph theory that will lay the basis for us to be able to explore more complex topics in future posts. The idea is to present the definitions along with visual representations, to help in the process of learning new concepts.
At the end of the post, you will find some algorithmic challenges so you can try and apply some of the topics that I will explain today. Feel free to skip to that part if you think you already have the necessary skills to solve them.
Algorithmically Speaking is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.
Nodes, Edges, and Paths
A graph consists of nodes and edges. We could see the nodes as the fundamental entities of graphs, and edges represent the relationships between nodes.
One good thing about graphs is that, even if they represent an abstract mathematical definition, we can visually display them using the most simple drawings of circles and lines.
For example, here’s a hand-drawn image of a graph with 5 nodes and 7 edges that we will we use to help us understand some basic definitions:
A path leads from node a to node b through the edges of the graph. The length of a path is the number of edges in it.
For example, the highlighted (red) path between the two green nodes in the following image is of length 3:
Note: The highlighted path is just one of the possible paths between the two green nodes. Try to identify every possible path between them.
A path is a cycle if the first and last node is the same. On the other hand, a path is called simple if each node appears at most once in the path.
For example, the highlighted path in the previous image corresponds to a simple path, while the one in the following image is considered a cycle:
A graph is connected if there is a path between any two nodes. For example, our initial graph is connected:
Try it yourself: take any pair of nodes in the previous graph and check that, indeed, there is a path between them.
On the other hand, the following graph is not connected because there is no path between the highlighted nodes:
In fact, there is no path between any node located in the right-bottom corner and any node located in the left-top corner.
The sets of connected nodes in a graph are called connected components. Here are the connected components (nodes with the same color) of the previous graph:
We can clearly see that this graph has two connected components.
A tree is a connected graph that consists of n nodes and n−1 edges. There is a unique path between any two nodes of a tree. You can also think of a tree as a connected graph that doesn’t contain any cycles.
For example, the following graph is a tree:
Try it yourself: take any pair of nodes in the previous graph and check that, indeed, there is a unique path between them.
Direction and Weights
A graph is directed if the edges can be traversed in one direction only. For example, the following graph is directed:
Notice how in the following image there is a path (more than one actually) between the green node and the blue node, but the opposite is not true. You cannot find any path starting in the blue node and ending in the green one:
As we can see, the fact the graph is directed affects the concept of connectivity, which is called strong connectivity when we are referring to directed graphs. We will learn more about this in future posts of the series.
In a weighted graph, each edge is assigned a weight. The weights are often interpreted as edge lengths.
The following graph is weighted:
The length of a path in a weighted graph is the sum of the edge weights on the path. For example, in the following graph, the length of the highlighted path between the green nodes is 13 (8 + 5):
In future posts, we will explore algorithms related to connectivity, we will dig deeper into the importance of weights and will learn some definitions that only make sense when we are talking about directed graphs.
In the meantime, it’s time to try what you’ve learned!
Test your Skills
Now that we know some basic definitions of graph theory, I will leave you with two challenges that you can think about during the week.
We can discuss the answers to these problems as a community and you can share your answers privately by replying to this email.
Here we go:
Problem #1: On the previous weighted graph, find a path with the minimum sum between the highlighted (green) nodes.
Problem #2: What happens to the connectivity of a tree if we remove any of its edges?
I hope I was able to ignite your passion for Graph theory by driving you through some of the basic definitions that will allow us to build the foundations for learning more complex concepts and applications of graphs.
A graph consists of nodes, and edges which represent relations between the nodes. A path is a sequence of nodes connected by edges. If the path starts and ends on the same node, it is called a cycle.
A graph is connected if there is a path between any pair of nodes. Otherwise, it is called not connected. The sets of connected nodes are called the connected components of the graph. A tree is a graph that is connected and doesn’t contain cycles.
Edges in a graph can have direction and weight. The weight of a path is the sum of the weights of the edges that belong to the path.
I wish you good luck when solving this week’s challenges. Don’t forget to share your progress with the community!
And if you think this post can be helpful for someone, share it with them. Nothing will make me happier!
See you next week!
Thanks for reading Algorithmically Speaking! Subscribe for free to receive new posts and support my work.