Knowee
Questions
Features
Study Tools

Graph Representation - Linked ListWrite  full code to represent the graph using linked list for the given input Note : Create undirected graphFor Example:input:5 70 10 41 21 31 42 33 4output:0 : -> 1 ->4 ->1 : -> 2 ->3 ->4 ->2 : -> 3 ->3 : -> 4 ->4 : ->Here vertex 0 is connected to vertex 1 and 41 is connected to 2,3,42 is connected to  33 is connected to 4

Question

Graph Representation - Linked ListWrite  full code to represent the graph using linked list for the given input Note : Create undirected graphFor Example:input:5 70 10 41 21 31 42 33 4output:0 : -> 1 ->4 ->1 : -> 2 ->3 ->4 ->2 : -> 3 ->3 : -> 4 ->4 : ->Here vertex 0 is connected to vertex 1 and 41 is connected to 2,3,42 is connected to  33 is connected to 4

🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a Python code to represent the graph using linked list:

class Node:
    def __init__(self, value):
        self.vertex = value
        self.next = None

class Graph:
    def __init__(self, vertices):
        self.V = vertices
        self.graph = [None] * self.V

    def add_edge(self, src, dest):
        node = Node(dest)
        node.next = self.graph[src]
        self.graph[src] = node

        node = Node(src)
        node.next = self.graph[dest]
        self.graph[dest] = node

    def print_graph(self):
        for i in range(self.V):
            print("Vertex " + str(i) + ":", end="")
            temp = self.graph[i]
            while temp:
                print(" -> {}".format(temp.vertex), end="")
                temp = temp.next
            print(" \n")

if __name__ == "__main__":
    V = 5
    graph = Graph(V)
    graph.add_edge(0, 1)
    graph.add_edge(0, 4)
    graph.add_edge(1, 2)
    graph.add_edge(1, 3)
    graph.add_edge(1, 4)
    graph.add_edge(2, 3)
    graph.add_edge(3, 4)

    graph.print_graph()

This code first defines a Node class for the linked list, and then a Graph class that uses this Node class to create a graph. The Graph class has methods to add an edge and to print the graph. The main function creates a graph with 5 vertices and adds edges between them as per the given input. Finally, it prints the

This problem has been solved

Similar Questions

Conversion of Graph from Matrix Representation to Linked List representationWrite a program to convert the given graph from matrix representation to linked list representationSample Input5 50 1 0 0 10 0 1 1 10 0 0 1 00 0 0 0 10 0 0 0 0output0 : -> 1 ->4 ->1 : -> 2 ->3 ->4 ->2 : -> 3 ->3 : -> 4 ->4 : ->

5 70 10 41 21 31 42 33 4output:0 : -> 1 ->4 ->1 : -> 2 ->3 ->4 ->2 : -> 3 ->3 : -> 4 ->4 : ->Here vertex 0 is connected to vertex 1 and 41 is connected to 2,3,42 is connected to  33 is connected to 4

Write a program to convert the given graph from matrix representation to linked list representation

Problem StatementImplement a Graph using an Adjacency List. Given the number of vertices (V) and the edges, create an undirected graph using an adjacency list. Print the adjacency list of each vertex.Input format :The first line of input consists of an integer V, representing the number of vertices.The second line consists of an integer E, representing the number of edges.The following E lines consist of the edge information.Output format :The output prints the adjacency list of each vertex.Refer to the sample output for formatting specifications.Code constraints :1 ≤ V ≤ 1001 ≤ E ≤ 100Sample test cases :Input 1 :570 10 41 21 31 42 33 4Output 1 :Adjacency list of vertex 0head -> 1-> 4Adjacency list of vertex 1head -> 0-> 2-> 3-> 4Adjacency list of vertex 2head -> 1-> 3Adjacency list of vertex 3head -> 1-> 2-> 4Adjacency list of vertex 4head -> 0-> 1-> 3Input 2 :671 31 41 52 32 42 54 6Output 2 :Adjacency list of vertex 0head Adjacency list of vertex 1head -> 3-> 4-> 5Adjacency list of vertex 2head -> 3-> 4-> 5Adjacency list of vertex 3head -> 1-> 2Adjacency list of vertex 4head -> 1-> 2-> 6Adjacency list of vertex 5head -> 1-> 2Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.

draw the three (3) types of linked list. Make sure the elements or nodes are related to each other.

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.