ในบทความนี้ เราจะเข้าใจวิธีการใช้โครงสร้างข้อมูลกราฟ เราใช้โครงสร้างข้อมูลกราฟ เราใช้กราฟใน Java โดยใช้คอลเลกชัน HashMap องค์ประกอบ HashMap อยู่ในรูปแบบของคู่คีย์-ค่า เราสามารถแสดงรายการที่อยู่ติดกันของกราฟใน HashMap ได้
ด้านล่างนี้เป็นการสาธิตสิ่งเดียวกัน -
สมมติว่าข้อมูลที่เราป้อนคือ −
Number of Vertices: 5 Number of edges: 5
ผลลัพธ์ที่ต้องการจะเป็น −
The connections between the nodes of the Graph are: 1 - 2 1 - 3 1 - 4 2 - 4 2 - 5 3 - 4 3 - 5 4 - 5
อัลกอริทึม
Step 1 - START Step 2 - Declare an object of a Graph class namely graph_object, two integers in class ‘Edge’ namely source and destination, and two integers in ‘main’ function namely vertices_count, edges_count. Step 3 - Define the values. Step 4 - Initialize values for the vertices and count. Step 5 - Create a new instance of the previously defined class. Step 6 - Initialize the instance with relevant values. Step 7 - Iterate over the instance using a ‘for’ loop, and display the output on the console. Step 8 - Display the result Step 9 - Stop
ตัวอย่างที่ 1
ที่นี่ เรารวมการดำเนินการทั้งหมดเข้าด้วยกันภายใต้ฟังก์ชัน 'หลัก'
public class Graph {
class Edge {
int source, destination;
}
int vertices, edges;
Edge[] edge;
Graph(int vertices, int edges) {
this.vertices = vertices;
this.edges = edges;
edge = new Edge[edges];
for(int i = 0; i < edges; i++) {
edge[i] = new Edge();
}
}
public static void main(String[] args) {
int vertices_count = 5;
int edges_count = 8;
Graph graph_object = new Graph(vertices_count, edges_count);
System.out.println("A graph object is defined.");
graph_object.edge[0].source = 1;
graph_object.edge[0].destination = 2;
graph_object.edge[1].source = 1;
graph_object.edge[1].destination = 3;
graph_object.edge[2].source = 1;
graph_object.edge[2].destination = 4;
graph_object.edge[3].source = 2;
graph_object.edge[3].destination = 4;
graph_object.edge[4].source = 2;
graph_object.edge[4].destination = 5;
graph_object.edge[5].source = 3;
graph_object.edge[5].destination = 4;
graph_object.edge[6].source = 3;
graph_object.edge[6].destination = 5;
graph_object.edge[7].source = 4;
graph_object.edge[7].destination = 5;
System.out.println("The connections between the edges of the Graph are: ");
for(int i = 0; i < edges_count; i++) {
System.out.println(graph_object.edge[i].source + " - " + graph_object.edge[i].destination);
}
}
} ผลลัพธ์
A graph object is defined. The connections between the edges of the Graph are: 1 - 2 1 - 3 1 - 4 2 - 4 2 - 5 3 - 4 3 - 5 4 - 5
ตัวอย่างที่ 2
ในที่นี้ เราสรุปการดำเนินการเป็นฟังก์ชันที่แสดงการเขียนโปรแกรมเชิงวัตถุ
public class Graph {
class Edge {
int source, destination;
}
int vertices, edges;
Edge[] edge;
Graph(int vertices, int edges) {
this.vertices = vertices;
this.edges = edges;
edge = new Edge[edges];
for(int i = 0; i < edges; i++) {
edge[i] = new Edge();
}
}
static void print(Graph graph_object,int edges_count){
System.out.println("The connections between the edges of the Graph are: ");
for(int i = 0; i < edges_count; i++) {
System.out.println(graph_object.edge[i].source + " - " + graph_object.edge[i].destination);
}
}
static void connect_edges(Graph graph_object){
graph_object.edge[0].source = 1;
graph_object.edge[0].destination = 2;
graph_object.edge[1].source = 1;
graph_object.edge[1].destination = 3;
graph_object.edge[2].source = 1;
graph_object.edge[2].destination = 4;
graph_object.edge[3].source = 2;
graph_object.edge[3].destination = 4;
graph_object.edge[4].source = 2;
graph_object.edge[4].destination = 5;
graph_object.edge[5].source = 3;
graph_object.edge[5].destination = 4;
graph_object.edge[6].source = 3;
graph_object.edge[6].destination = 5;
graph_object.edge[7].source = 4;
graph_object.edge[7].destination = 5;
}
public static void main(String[] args) {
int vertices_count = 5;
int edges_count = 8;
Graph graph_object = new Graph(vertices_count, edges_count);
System.out.println("A graph object is defined.");
connect_edges(graph_object);
print(graph_object, edges_count);
}
} ผลลัพธ์
A graph object is defined. The connections between the edges of the Graph are: 1 - 2 1 - 3 1 - 4 2 - 4 2 - 5 3 - 4 3 - 5 4 - 5