0
0
Data Structures Theoryknowledge~30 mins

Directed vs undirected graphs in Data Structures Theory - Hands-On Comparison

Choose your learning style9 modes available
Understanding Directed vs Undirected Graphs
📖 Scenario: You are helping a friend understand how different types of graphs work by creating simple examples of directed and undirected graphs. These graphs represent connections between places in a small town.
🎯 Goal: Build two simple graph examples: one directed and one undirected. This will help visualize how connections differ when direction matters versus when it does not.
📋 What You'll Learn
Create a dictionary called undirected_graph representing an undirected graph with exact nodes and edges
Create a dictionary called directed_graph representing a directed graph with exact nodes and edges
Add a variable called node_count that counts the total number of nodes in the graphs
Add a variable called edge_count that counts the total number of edges in the directed graph
💡 Why This Matters
🌍 Real World
Graphs are used to model networks like roads, social connections, and computer networks where direction of connection matters or not.
💼 Career
Understanding graph types is important for roles in software development, data science, and network engineering.
Progress0 / 4 steps
1
Create an undirected graph
Create a dictionary called undirected_graph with these exact connections: 'A': ['B', 'C'], 'B': ['A', 'D'], 'C': ['A'], and 'D': ['B']. This represents an undirected graph where edges go both ways.
Data Structures Theory
Need a hint?

Think of each key as a place and the list as places directly connected to it without direction.

2
Create a directed graph
Add a dictionary called directed_graph with these exact connections: 'A': ['B', 'C'], 'B': ['D'], 'C': [], and 'D': ['A']. This represents a directed graph where edges have direction from the key to the nodes in the list.
Data Structures Theory
Need a hint?

Remember, in a directed graph, connections only go one way from the key to the listed nodes.

3
Count the total nodes
Create a variable called node_count that stores the total number of unique nodes in the graphs. Use the keys of the undirected_graph dictionary to count nodes.
Data Structures Theory
Need a hint?

Use the len() function on the keys of the undirected graph dictionary.

4
Count the total edges in the directed graph
Create a variable called edge_count that stores the total number of edges in the directed_graph. Calculate this by summing the lengths of all adjacency lists in directed_graph.
Data Structures Theory
Need a hint?

Use a generator expression inside sum() to add up the lengths of all adjacency lists.