📖 Scenario: Imagine you are organizing a small social network where people are connected as friends. You want to represent these connections using a graph.
🎯 Goal: You will create a simple graph using Java to represent people as vertices and their friendships as edges. You will also mark if the friendships are one-way (directed) or two-way (undirected), and if the connections have a strength (weight).
📋 What You'll Learn
Create a graph with exactly 4 vertices named "Alice", "Bob", "Charlie", and "Diana"
Create edges between these vertices with specified directions and weights
Use a variable to indicate if the graph is directed or undirected
Store weights as integers representing the strength of the connection
Print the graph connections showing vertices, edges, direction, and weights
💡 Why This Matters
🌍 Real World
Graphs are used to model social networks, maps, and many relationships in real life.
💼 Career
Understanding graph basics is essential for software roles involving networking, recommendation systems, and data analysis.
Progress0 / 4 steps
1
Create vertices for the graph
Create a String[] array called vertices with these exact names: "Alice", "Bob", "Charlie", and "Diana".
DSA Java
Hint
Use curly braces {} to list the names inside the array.
2
Set graph type as directed or undirected
Create a boolean variable called isDirected and set it to true to indicate the graph is directed.
DSA Java
Hint
Use boolean type and assign true or false.
3
Create edges with weights
Create a 2D integer array called weights representing the edges between vertices. Use these exact values: weights[0][1] = 5 (Alice to Bob), weights[1][2] = 3 (Bob to Charlie), weights[2][3] = 4 (Charlie to Diana), weights[3][0] = 2 (Diana to Alice). Set all other edges to 0.
DSA Java
Hint
Initialize a 4x4 integer array and assign weights to specific edges.
4
Print graph edges with direction and weights
Use nested for loops to print all edges with weight > 0 in this exact format: "Alice -> Bob : 5" if isDirected is true, otherwise use "Alice -- Bob : 5". Print one edge per line.
DSA Java
Hint
Use nested loops to check all pairs and print only edges with weight > 0. Use System.out.println for output.
Practice
(1/5)
1. Which of the following best describes a vertex in a graph?
easy
A. A point or node where edges meet
B. A line connecting two vertices
C. A graph with no edges
D. A graph that has weights on edges
Solution
Step 1: Understand the definition of vertex
A vertex is a fundamental unit in a graph representing a point or node.
Step 2: Differentiate vertex from edges and graph types
Edges connect vertices; weights and graph types describe edges or the whole graph, not vertices.
Final Answer:
A point or node where edges meet -> Option A
Quick Check:
Vertex = point/node [OK]
Hint: Vertices are points/nodes, edges are connections [OK]
Common Mistakes:
Confusing edges with vertices
Thinking weights define vertices
Mixing graph types with vertex definition
2. Which of the following is the correct way to declare an undirected weighted graph using adjacency list in Java?
easy
A. Map graph = new HashMap<>();
B. Map graph = new TreeMap<>();
C. Map>> graph = new TreeSet<>();
D. Map>> graph = new HashMap<>();
Solution
Step 1: Identify data structure for weighted edges
Weighted edges need to store both neighbor and weight, so Pair is used.
Step 2: Choose correct map and list types
HashMap with List of pairs is standard for adjacency list of weighted graphs.
Final Answer:
Map>> graph = new HashMap<>(); -> Option D
Quick Check:
Weighted graph uses Pair for edges [OK]
Hint: Weighted edges need pairs: neighbor and weight [OK]
Common Mistakes:
Using List<Integer> for weighted edges
Using TreeSet instead of List for adjacency
Confusing Map types for graph
3. Consider the following Java code snippet for a directed graph: