Join Regular Classroom : Visit ClassroomTech

Data Structure – codewindow.in

Related Topics

Data Structure

graph = {
    'A': ['B', 'C'],
    'B': ['D', 'E'],
    'C': ['F'],
    'D': [],
    'E': ['F'],
    'F': []
}

In this example, the graph has six vertices labeled ‘A’ through ‘F’. The dictionary ‘graph’ represents the adjacency list, where each key-value pair represents a vertex and its adjacent vertices.

  1. Adjacency Matrix: This representation stores the graph as a two-dimensional matrix, where each row and column represent a vertex in the graph, and each element of the matrix represents an edge between two vertices. If the graph is unweighted, the element is typically a 1 or 0, indicating the presence or absence of an edge between the vertices. If the graph is weighted, the element contains the weight or cost of the edge.

Here is an example of representing a graph using an adjacency matrix in Python:

graph = [
    [0, 1, 1, 0, 0, 0],
    [1, 0, 0, 1, 1, 0],
    [1, 0, 0, 0, 0, 1],
    [0, 1, 0, 0, 0, 0],
    [0, 1, 0, 0, 0, 1],
    [0, 0, 1, 0, 1, 0]
]

In this example, the graph has six vertices labeled 0 through 5. The matrix ‘graph’ represents the adjacency matrix, where each row and column represent a vertex and each element of the matrix represents an edge between two vertices. A value of 1 indicates the presence of an edge, and a value of 0 indicates the absence of an edge.

      

Go through our study material. Your Job is awaiting.

Recent Posts
Categories