先决条件:图形及其表示
给定一个图的邻接矩阵表示。任务是将给定的 Adjacency Matrix 转换为 Adjacency List 表示。
例子:
Input: arr[][] = [ [0, 0, 1], [0, 0, 1], [1, 1, 0] ]
Output: The adjacency list is:
0 -> 2
1 -> 2
2 -> 0 -> 1
Input: arr[][] = [ [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] ]
Output: The adjacency list is:
0 -> 1 -> 4
1 -> 0 -> 2 -> 3 -> 4
2 -> 1 -> 3
3 -> 1 -> 2 -> 4
4 -> 0 -> 1 -> 3
邻接矩阵:邻接矩阵是一个大小为 V x V 的二维数组,其中 V 是图中的顶点数。设二维数组为 adj[][],槽 adj[i][j] = 1 表示从顶点 i 到顶点 j 有一条边。
邻接列表:使用列表数组。数组的大小等于顶点的数量。令数组为array[]。条目 array[i] 表示与第 i 个顶点相邻的顶点列表。
将邻接矩阵转换为邻接表。创建一个列表数组并遍历邻接矩阵。如果对于矩阵“ mat[i][j] = 1 ”中的任何单元格(i, j) ,则表示从i 到 j存在边,因此将j插入列表中的数组中的第 i 个位置列表。
下面是上述方法的实现:
C++
#include
using namespace std;
// CPP program to convert Adjacency matrix
// representation to Adjacency List
// converts from adjacency matrix to adjacency list
vector> convert( vector> a)
{
vector> adjList(a.size());
for (int i = 0; i < a.size(); i++)
{
for (int j = 0; j < a[i].size(); j++)
{
if (a[i][j] == 1)
{
adjList[i].push_back(j);
}
}
}
return adjList;
}
// Driver code
int main()
{
vector> a;
vector p({0, 0, 1});
vector q({0, 0, 1});
vector r({1, 1, 0}); // adjacency matrix
a.push_back(p);
a.push_back(q);
a.push_back(r);
vector> AdjList = convert(a);
cout<<"Adjacency List:"< " << AdjList[i][j] << endl;
break;
}
else
cout << " -> " << AdjList[i][j];
}
}
}
// This code is contributed by Surendra_Gangwar
Java
// Java program to convert adjacency
// matrix representation to
// adjacency list
import java.util.*;
public class GFG {
// Function to convert adjacency
// list to adjacency matrix
static ArrayList> convert(int[][] a)
{
// no of vertices
int l = a[0].length;
ArrayList> adjListArray
= new ArrayList>(l);
// Create a new list for each
// vertex such that adjacent
// nodes can be stored
for (int i = 0; i < l; i++) {
adjListArray.add(new ArrayList());
}
int i, j;
for (i = 0; i < a[0].length; i++) {
for (j = 0; j < a.length; j++) {
if (a[i][j] == 1) {
adjListArray.get(i).add(j);
}
}
}
return adjListArray;
}
// Function to print the adjacency list
static void printArrayList(ArrayList>
adjListArray)
{
// Print the adjacency list
for (int v = 0; v < adjListArray.size(); v++) {
System.out.print(v);
for (Integer u : adjListArray.get(v)) {
System.out.print(" -> " + u);
}
System.out.println();
}
}
// Driver Code
public static void main(String[] args)
{
// Given Adjacency Matrix
int[][] a = { { 0, 0, 1 },
{ 0, 0, 1 },
{ 1, 1, 0 } };
// function to convert adjacency
// list to adjacency matrix
ArrayList> adjListArray = convert(a);
System.out.println("Adjacency List: ");
printArrayList(adjListArray);
}
}
Python
# Python program to convert Adjacency matrix
# representation to Adjacency List
from collections import defaultdict
# converts from adjacency matrix to adjacency list
def convert(a):
adjList = defaultdict(list)
for i in range(len(a)):
for j in range(len(a[i])):
if a[i][j]== 1:
adjList[i].append(j)
return adjList
# driver code
a =[[0, 0, 1], [0, 0, 1], [1, 1, 0]] # adjacency matrix
AdjList = convert(a)
print("Adjacency List:")
# print the adjacency list
for i in AdjList:
print(i, end ="")
for j in AdjList[i]:
print(" -> {}".format(j), end ="")
print()
# This code is contributed by Muskan Kalra.
C#
// C# program to convert adjacency
// matrix representation to
// adjacency list
using System;
using System.Collections.Generic;
class GFG
{
// Function to convert adjacency
// list to adjacency matrix
static List> convert(int[,] a)
{
// no of vertices
int l = a.GetLength(0);
List> adjListArray = new List>(l);
int i, j;
// Create a new list for each
// vertex such that adjacent
// nodes can be stored
for (i = 0; i < l; i++)
{
adjListArray.Add(new List());
}
for (i = 0; i < a.GetLength(0); i++)
{
for (j = 0; j < a.GetLength(1); j++)
{
if (a[i,j] == 1)
{
adjListArray[i].Add(j);
}
}
}
return adjListArray;
}
// Function to print the adjacency list
static void printList(List> adjListArray)
{
// Print the adjacency list
for (int v = 0; v < adjListArray.Count; v++)
{
Console.Write(v);
foreach (int u in adjListArray[v])
{
Console.Write(" -> " + u);
}
Console.WriteLine();
}
}
// Driver Code
public static void Main(String[] args)
{
// Given Adjacency Matrix
int[,] a = { { 0, 0, 1 }, { 0, 0, 1 }, { 1, 1, 0 } };
// function to convert adjacency
// list to adjacency matrix
List> adjListArray = convert(a);
Console.WriteLine("Adjacency List: ");
printList(adjListArray);
}
}
// This code is contributed by 29AjayKumar
Adjacency List:
0 -> 2
1 -> 2
2 -> 0 -> 1
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live