Professional Documents
Culture Documents
Lab 9
Graph
Quang D. C.
dcquang@it.tdt.edu.vn
• Adjacency Matrix
• Adjacency List
• Edge List
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
• Adjacency Matrix
• Adjacency List
• Edge List
dcquang@it.tdt.edu.vn 1
Ton Duc Thang University
Faculty of Information Technology Data Structures and Algorithms, Fall 2020-2021
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.
dcquang@it.tdt.edu.vn 2
Ton Duc Thang University
Faculty of Information Technology Data Structures and Algorithms, Fall 2020-2021
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
5 public AdjacencyMatrix (int vertices ){
6 NUMBER_OF_VERTICES = vertices ;
7 adj = new int[ NUMBER_OF_VERTICES ][ NUMBER_OF_VERTICES ];
8 }
9
10 public void setEgde (int vertexSource , int vertexDestination ,
int weight ){
11 try {
12 adj[ vertexSource ][ vertexDestination ] = weight ;
13 adj[ vertexDestination ][ vertexSource ] = weight ;
14 } catch ( ArrayIndexOutOfBoundsException indexBounce ){
15 System .out. println ("The vertex is invalid ");
16 }
17 }
18
19 public int getEgde (int vertexSource , int vertexDestination ){
20 try{
21 return adj[ vertexSource ][ vertexDestination ];
22 } catch ( ArrayIndexOutOfBoundsException indexBounce ){
23 System .out. println ("The vertex is invalid ");
24 }
25 return -1;
26 }
27
28 public void printGraph (){
29 for(int i = 0; i < NUMBER_OF_VERTICES ; i++){
30 for(int j = 0;j < NUMBER_OF_VERTICES ; j++){
31 System .out. print (adj[i][j] + " ");
32 }
33 System .out. println ();
34 }
35 }
36 }
dcquang@it.tdt.edu.vn 3
Ton Duc Thang University
Faculty of Information Technology Data Structures and Algorithms, Fall 2020-2021
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 public AdjacencyList (int v)
7 {
8 V = v;
9 adj = new LinkedList [v];
10 for (int i=0; i<v; ++i)
11 adj[i] = new LinkedList <Integer >();
12 }
13
14 public void addEdge (int u, int v)
15 {
16 adj[u]. add(v);
17 }
18
19 public void printGraph (){
20 for(int i = 0; i < V; i++){
21 System .out. print (" Vertex " + i + ": ");
22 System .out. print ("head");
23 for( Integer v: adj[i]){
24 System .out. print ("->" + v);
25 }
26 System .out. println ();
27 }
28 }
29 }
dcquang@it.tdt.edu.vn 4
Ton Duc Thang University
Faculty of Information Technology Data Structures and Algorithms, Fall 2020-2021
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;
9 dest = d;
10 }
11
12 public Integer getWeight (){
13 return weight ;
14 }
15
16 public Integer getSource (){
17 return source ;
18 }
19
dcquang@it.tdt.edu.vn 5
Ton Duc Thang University
Faculty of Information Technology Data Structures and Algorithms, Fall 2020-2021
36
37 public void addEdge (int w, int u, int v){
38 edges .add(new IntegerTriple (w,u,v));
39 }
40
41 public void printGraph (){
42 for(int i = 0; i < edges .size (); i++){
43 System .out. println ( edges .get(i));
44 }
45 }
46 }
dcquang@it.tdt.edu.vn 6
Ton Duc Thang University
Faculty of Information Technology Data Structures and Algorithms, Fall 2020-2021
11 }
12
13 public void DFS(int s){
14 boolean [] visited = new boolean [ NUMBER_OF_VERTICES ];
15 DFS_recur (s, visited );
16 }
4. Exercise
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.
Exercise 2
A graph is saved to file in AL format: (see the picture below)
dcquang@it.tdt.edu.vn 7
Ton Duc Thang University
Faculty of Information Technology Data Structures and Algorithms, Fall 2020-2021
(a) Read the graph from the file and print the AL on the screen.
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.
dcquang@it.tdt.edu.vn 8
Ton Duc Thang University
Faculty of Information Technology Data Structures and Algorithms, Fall 2020-2021
(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.
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
dcquang@it.tdt.edu.vn 9