Classwork: Lab 9 Graph
Classwork: Lab 9 Graph
Lab 9
Graph
Quang D. C.
[email protected]
Note
In this lab, we will learn how to represent a graph on the computer:
• Adjacency Matrix
• Adjacency List
• Edge List
And two ways to traverse a graph:
• Breadth First Search (BFS)
• Depth First Search (DFS)
Part I
Classwork
In this part, lecturer will:
• During these part, students may ask any question that they don’t understand or
make mistakes. Lecturers can guide students, or do general guidance for the whole
class if the errors are common.
[email protected] 1
Ton Duc Thang University
Faculty of Information Technology Data Structures and Algorithms, Fall 2021-2022
1. What is Graph?
Graph is the data structure that included vertices and edges. In other words, graph
is a set of vertices where some pairs of vertices are connected by edges.
Figure 1: Graph
2. Representations of Graph
2.1. Adjacency Matrix (AM)
Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in
a graph.
Let the 2D array be 𝑎𝑑𝑗[][], a slot 𝑎𝑑𝑗[𝑖][𝑗] = 1 indicates that there is an edge from
vertex 𝑖 to vertex 𝑗, otherwise 𝑎𝑑𝑗[𝑖][𝑗] contains 0.
[email protected] 2
Ton Duc Thang University
Faculty of Information Technology Data Structures and Algorithms, Fall 2021-2022
You can create an AdjacencyMatrix class and define the code like this:
1 public class AdjacencyMatrix {
2 private int [][] adj;
3 private final int NUMBER_OF_VERTICES ;
4
[email protected] 3
Ton Duc Thang University
Faculty of Information Technology Data Structures and Algorithms, Fall 2021-2022
In Java, there are many collections that you can use to implement Adjacency List
like Vector, LinkedList, ArrayList, Map, ... Remember import java.util.* if you want
to use Java collections.
We use LinkedList to implement an AdjacencyList class (this code for the graph
without weights. If you want to handle weights, you can use Pair<Integer, Integer> in
Java instead of Integer):
1 import java.util .*;
2
3 public class AdjacencyList {
4 private int V; // No. of vertices
5 private LinkedList <Integer > adj [];
6
[email protected] 4
Ton Duc Thang University
Faculty of Information Technology Data Structures and Algorithms, Fall 2021-2022
There are many data structures to implement Edge List, this code use Vector and
class IntegerTriple, which is defined by user:
1 class IntegerTriple {
2 private Integer weight ;
3 private Integer source ;
4 private Integer dest;
5
6 public IntegerTriple ( Integer w, Integer s, Integer d){
7 weight = w;
8 source = s;
[email protected] 5
Ton Duc Thang University
Faculty of Information Technology Data Structures and Algorithms, Fall 2021-2022
9 dest = d;
10 }
11
12 public Integer getWeight (){
13 return weight ;
14 }
15
16 public Integer getSource (){
17 return source ;
18 }
19
20 public Integer getDest (){
21 return dest;
22 }
23
24 @Override
25 public String toString (){
26 return weight + " " + source + " " + dest;
27 }
28 }
29
30 public class EdgeList {
31 private Vector < IntegerTriple > edges ;
32
33 public EdgeList (){
34 edges = new Vector < IntegerTriple >();
35 }
36
[email protected] 6
Ton Duc Thang University
Faculty of Information Technology Data Structures and Algorithms, Fall 2021-2022
Part II
Excercise
Responsibility of the students in this part:
Exercise 1
A graph is saved to file in AM format: (see the picture below)
(a) Read the graph from the file and print the AM on the screen.
[email protected] 7
Ton Duc Thang University
Faculty of Information Technology Data Structures and Algorithms, Fall 2021-2022
Exercise 2
A graph is saved to file in AL format: (see the picture below)
(a) Read the graph from the file and print the AL on the screen.
[email protected] 8
Ton Duc Thang University
Faculty of Information Technology Data Structures and Algorithms, Fall 2021-2022
Exercise 3
A graph is saved to file in EL format: (see the picture below)
(a) Read the graph from the file and print the EL on the screen.
Exercise 4
Choose a graph, and use Adjacency Matrix to represent it. Implement the functions
for the below requirements:
(a) Traverse the graph by using BFS. Print the traversal result on the screen.
(b) Traverse the graph by using DFS. Print the traversal result on the screen.
(c) Implement the DFS method without recursion. Print the traversal result on the
screen.(Hint: using Stack)
(d) Implement the IsReachable method to test whether vertex v is reachable from vertex
u.
[email protected] 9
Ton Duc Thang University
Faculty of Information Technology Data Structures and Algorithms, Fall 2021-2022
Exercise 5
Write a method in the AdjacencyMatrix class to convert a graph from Adjacency
Matrix to Adjacency List.
1 public AdjacencyList convertToAL (){
2 ...
3 }
THE END