Convert Adjacency List to Adjacency Matrix representation of a Graph

Convert Adjacency List to Adjacency Matrix representation of a Graph

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive.

Given an adjacency list representation of a Graph, the task is to convert the given Adjacency List to Adjacency Matrix representation.

Examples: 

Input:_ adjList[] = {{0 –> 1 –> 3}, {1 –> 2}, {2 –> 3}} _

Output:

0 1 0 1

0 0 1 0

0 0 0 1

0 0 0 0

Input:_ adjList[] = {{0 –> 1 –> 4}, {1 –> 0 –> 2 –> 3 –> 4}, {2 –> 1 –> 3}, {3 –> 1 –> 2 –> 4}, {4 –> 0 –> 1 –> 3}} _

Output:

0 1 0 0 1

1 0 1 1 1

0 1 0 1 0

0 1 1 0 1

_1 1 0 1 0 _

Recommended: Please try your approach on _{IDE}_ first, before moving on to the solution.

Adjacency List: An array of lists is used. The size of the array is equal to the number of vertices. Let the array be an array[]. An entry array[i] represents the list of vertices adjacent to the ith Vertex.

Adjacency Matrix: 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 adj[][], a slot adj[i][j] = 1 indicates that there is an edge from vertex i to vertex j.

Follow the steps below to convert an adjacency list to an adjacency matrix: 

  • Initialize a matrix with 0s.
  • Iterate over the vertices in the adjacency list
  • For every jth vertex in the adjacency list, traverse its edges.
  • For each vertex i with which the jth vertex has an edge, set mat[i][j] = 1.

data structures graph graph traversals graph-basics data analysis

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

A Complete Introduction to Graph Data Structure

We will discuss the Graph Data Structure: definition, types and examples. Data structures are important for storing data in efficient ways.

7 Techniques to Clean and Structure Data for Analysis

This is because AI and analytics tools are very picky: The data has to be in just the right format, and anything unexpected throws a wrench into the system.

Exploratory Data Analysis is a significant part of Data Science

Data science is omnipresent to advanced statistical and machine learning methods. For whatever length of time that there is data to analyse, the need to investigate is obvious.

Tableau Data Analysis Tips and Tricks

Tableau Data Analysis Tips and Tricks. Master the one of the most powerful data analytics tool with some handy shortcut and tricks.

Analysis, Price Modeling and Prediction: AirBnB Data for Seattle.

Analysis, Price Modeling and Prediction: AirBnB Data for Seattle. A detailed overview of AirBnB’s Seattle data analysis using Data Engineering & Machine Learning techniques.