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! ๐ฏ
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.
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.
# Defining a simple graph with vertices
vertices = ['A', 'B', 'C', 'D', 'E']Edges are the connections between vertices in a graph. They represent the relationship between two nodes.
# Edges represented as tuples (vertex1, vertex2)
edges = [('A', 'B'), ('A', 'C'), ('B', 'D'), ('C', 'E')]The degree of a vertex is the number of edges connected to it. There are two types of degrees:
Let's calculate the degree of each vertex in the following graph:
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)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.
Let's find the path between vertices 'A' and 'E' in the following graph:
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}))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.
Let's check if the given graph contains a cycle:
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.")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! ๐๐ป๐