Graph Terminology: Vertex, Edge, Degree, Path, Cycle ๐Ÿš€

beginner
8 min

Graph Terminology: Vertex, Edge, Degree, Path, Cycle ๐Ÿš€

Welcome to a new journey at CodeYourCraft! Today, we're diving into the fascinating world of Data Structures and Algorithms. In this lesson, we'll explore the core terminology of Graphs โ€“ Vertex, Edge, Degree, Path, and Cycle. Let's embark on this exciting journey! ๐ŸŽฏ

Understanding Graphs ๐Ÿ“

A graph is a structure consisting of nodes (also called vertices) and connections (edges). These nodes and edges are used to represent pairwise relations between objects in a way that's easy to understand and analyze.

Vertex ๐Ÿ”„

Vertices are the building blocks of a graph. Each vertex represents an object or a node in the data structure.

๐Ÿ’ก Pro Tip: Vertices can be represented as integers, characters, or even complex objects like classes in programming.

Example in Python ๐Ÿ

python
# Defining a simple graph with vertices vertices = ['A', 'B', 'C', 'D', 'E']

Edge ๐Ÿ”—

Edges are the connections between vertices in a graph. They represent the relationship between two nodes.

python
# Edges represented as tuples (vertex1, vertex2) edges = [('A', 'B'), ('A', 'C'), ('B', 'D'), ('C', 'E')]

Degree ๐Ÿ”ข

The degree of a vertex is the number of edges connected to it. There are two types of degrees:

  1. In-degree: The number of edges pointing towards a vertex.
  2. Out-degree: The number of edges pointing away from a vertex.

Example in Python ๐Ÿ

Let's calculate the degree of each vertex in the following graph:

python
vertices = ['A', 'B', 'C', 'D', 'E'] edges = [('A', 'B'), ('A', 'C'), ('B', 'D'), ('C', 'E')] # Calculating the degree of each vertex degrees = {vertex: edges.count(vertex) for vertex in vertices} print(degrees)

Path ๐Ÿ›ก๏ธ

A path is a sequence of edges connecting a series of vertices in a graph. A path goes from one vertex to another, and each edge in the path connects two consecutive vertices.

Example in Python ๐Ÿ

Let's find the path between vertices 'A' and 'E' in the following graph:

python
vertices = ['A', 'B', 'C', 'D', 'E'] edges = [('A', 'B'), ('A', 'C'), ('B', 'D'), ('C', 'E')] def find_path(start, end, graph): path = [] current_vertex = start while current_vertex != end: path.append(current_vertex) for vertex, neighbor in graph.items(): if neighbor == current_vertex: current_vertex = vertex break path.append(end) return path print(find_path('A', 'E', {vertex: [neighbor] for vertex, neighbor in edges}))

Cycle ๐Ÿ”

A cycle is a path in a graph where the first and last vertices are connected by an edge. In other words, a cycle is a path that starts and ends at the same vertex.

Example in Python ๐Ÿ

Let's check if the given graph contains a cycle:

python
vertices = ['A', 'B', 'C', 'D', 'E'] edges = [('A', 'B'), ('A', 'C'), ('B', 'D'), ('C', 'E'), ('D', 'C')] # Depth-first search to check for cycles def dfs(vertex, graph, visited, adjacency_list): visited.add(vertex) for neighbor in adjacency_list[vertex]: if neighbor not in visited: dfs(neighbor, graph, visited, adjacency_list) elif neighbor in visited: return True return False graph = {vertex: [neighbor for neighbor in edges if vertex == neighbor[0]] for vertex, neighbor in edges} visited = set() if dfs('A', graph, visited, graph): print("The graph contains a cycle.") else: print("The graph does not contain a cycle.")

Time to Practice! ๐Ÿงช

Quick Quiz
Question 1 of 1

What is a path in a graph?

Congratulations! You've now learned the essential terminology of Graphs โ€“ Vertex, Edge, Degree, Path, and Cycle. Keep exploring the fascinating world of Data Structures and Algorithms at CodeYourCraft! ๐Ÿš€ Happy learning! ๐Ÿ˜„๐Ÿ’ป๐ŸŽ‰