কম্পিউটার টিউটোরিয়াল

জাভা প্রোগ্রাম: গ্রাফ (Graph) ডেটা স্ট্রাকচার ইমপ্লিমেন্টেশন

এই আর্টিকেলে আমরা বুঝব কীভাবে গ্রাফ (Graph) ডেটা স্ট্রাকচার ইমপ্লিমেন্ট করতে হয়। আমরা জাভাতে 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 - Graph ক্লাসের একটি অবজেক্ট ডিক্লেয়ার করুন graph_object নামে, 'Edge' ক্লাসে দুটি ইন্টিজার source এবং destination, এবং 'main' ফাংশনে দুটি ইন্টিজার vertices_count, edges_count। 
Step 3 - মান ডিফাইন 
Step 4 - ভার্টেক্স এবং কাউন্টের জন্য মান ইনিশিয়ালাইজ 
Step 5 - পূর্বে ডিফাইন করা ক্লাসের নতুন ইনস্ট্যান্স তৈরি করুন 
Step 6 - ইনস্ট্যান্সটিকে প্রাসঙ্গিক মান দিয়ে ইনিশিয়ালাইজ করুন 
Step 7 - 'for' লুপ ব্যবহার করে ইনস্ট্যান্সের ওপর ইটারেট করুন এবং কনসোলে আউটপুট দেখান 
Step 8 - ফলাফল প্রদর্শন 
Step 9 - STOP

উদাহরণ ১

এখানে সকল অপারেশন 'main' ফাংশনের অধীনে বাঁধা হয়েছে।

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

উদাহরণ ২

এখানে অপারেশনগুলো ফাংশনে এনক্যাপসুলেট করা হয়েছে অবজেক্ট-ওরিয়েন্টেড প্রোগ্রামিং প্রদর্শন করতে।

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
  1. জাভা প্রোগ্রাম: বৃত্তের পরিধি (Perimeter) বের করার পদ্ধতি

  2. জাভা প্রোগ্রাম: ট্রাপিজিয়াম (Trapezium) এর ক্ষেত্রফল বের করার পদ্ধতি

  3. জাভা প্রোগ্রাম: আয়তের পরিসীমা নির্ণয়

  4. জাভাতে সংখ্যা গণনার প্রোগ্রাম কীভাবে বাস্তবায়ন করবেন?