📌  相关文章
📜  将邻接表转换为图的邻接矩阵表示

📅  最后修改于: 2021-09-02 05:56:05             🧑  作者: Mango

给定Graph的邻接表表示,任务是将给定的邻接表转换为邻接矩阵表示。

例子:

邻接列表:使用列表数组。数组的大小等于顶点的数量。让数组是一个数组[]。条目 array[i] 表示与第 i顶点相邻的顶点列表。

邻接矩阵:邻接矩阵是一个大小为 V x V 的二维数组,其中 V 是图中的顶点数。设二维数组为 adj[][],槽 adj[i][j] = 1 表示从顶点 i 到顶点 j 有一条边。

按照以下步骤将邻接表转换为邻接矩阵:

  • 0 s 初始化矩阵。
  • 迭代邻接表中的顶点
  • 对于邻接表中的每个j顶点,遍历它的边。
  • 对于第j顶点有边的每个顶点i ,设置 mat[i][j] = 1。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to insert vertices to adjacency list
void insert(vector adj[], int u, int v)
{
    // Insert a vertex v to vertex u
    adj[u].push_back(v);
    return;
}
 
// Function to display adjacency list
void printList(vector adj[], int V)
{
    for (int i = 0; i < V; i++) {
        cout << i;
        for (auto j : adj[i])
            cout << " --> " << j;
        cout << endl;
    }
    cout << endl;
}
 
// Function to convert adjacency
// list to adjacency matrix
vector > convert(vector adj[],
                             int V)
{
    // Initialize a matrix
    vector > matrix(V,
                                vector(V, 0));
 
    for (int i = 0; i < V; i++) {
        for (auto j : adj[i])
            matrix[i][j] = 1;
    }
    return matrix;
}
 
// Function to display adjacency matrix
void printMatrix(vector > adj, int V)
{
    for (int i = 0; i < V; i++) {
        for (int j = 0; j < V; j++) {
            cout << adj[i][j] << "   ";
        }
        cout << endl;
    }
    cout << endl;
}
 
// Driver code
int main()
{
    int V = 5;
 
    vector adjList[V];
 
    // Inserting edges
    insert(adjList, 0, 1);
    insert(adjList, 0, 4);
    insert(adjList, 1, 0);
    insert(adjList, 1, 2);
    insert(adjList, 1, 3);
    insert(adjList, 1, 4);
    insert(adjList, 2, 1);
    insert(adjList, 2, 3);
    insert(adjList, 3, 1);
    insert(adjList, 3, 2);
    insert(adjList, 3, 4);
    insert(adjList, 4, 0);
    insert(adjList, 4, 1);
    insert(adjList, 4, 3);
 
    // Display adjacency list
    cout << "Adjacency List: \n";
    printList(adjList, V);
 
    // Function call which returns
    // adjacency matrix after conversion
    vector > adjMatrix
        = convert(adjList, V);
 
    // Display adjacency matrix
    cout << "Adjacency Matrix: \n";
    printMatrix(adjMatrix, V);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to insert vertices to adjacency list
static void insert(Vector adj[],
                   int u, int v)
{
     
    // Insert a vertex v to vertex u
    adj[u].add(v);
    return;
}
 
// Function to display adjacency list
static void printList(Vector adj[],
                      int V)
{
    for(int i = 0; i < V; i++)
    {
        System.out.print(i);
 
        for(int j : adj[i])
            System.out.print(" --> " + j);
             
        System.out.println();
    }
    System.out.println();
}
 
// Function to convert adjacency
// list to adjacency matrix
static int[][] convert(Vector adj[],
                       int V)
{
     
    // Initialize a matrix
    int [][]matrix = new int[V][V];
 
    for(int i = 0; i < V; i++)
    {
        for(int j : adj[i])
            matrix[i][j] = 1;
    }
    return matrix;
}
 
// Function to display adjacency matrix
static void printMatrix(int[][] adj, int V)
{
    for(int i = 0; i < V; i++)
    {
        for(int j = 0; j < V; j++)
        {
            System.out.print(adj[i][j] + " ");
        }
        System.out.println();
    }
    System.out.println();
}
 
// Driver code
public static void main(String[] args)
{
    int V = 5;
 
    @SuppressWarnings("unchecked")
    Vector []adjList = new Vector[V];
    for(int i = 0; i < adjList.length; i++)
        adjList[i] = new Vector();
         
    // Inserting edges
    insert(adjList, 0, 1);
    insert(adjList, 0, 4);
    insert(adjList, 1, 0);
    insert(adjList, 1, 2);
    insert(adjList, 1, 3);
    insert(adjList, 1, 4);
    insert(adjList, 2, 1);
    insert(adjList, 2, 3);
    insert(adjList, 3, 1);
    insert(adjList, 3, 2);
    insert(adjList, 3, 4);
    insert(adjList, 4, 0);
    insert(adjList, 4, 1);
    insert(adjList, 4, 3);
 
    // Display adjacency list
    System.out.print("Adjacency List: \n");
    printList(adjList, V);
 
    // Function call which returns
    // adjacency matrix after conversion
    int[][] adjMatrix = convert(adjList, V);
 
    // Display adjacency matrix
    System.out.print("Adjacency Matrix: \n");
    printMatrix(adjMatrix, V);
}
}
 
// This code is contributed by amal kumar choubey


Python3
# Python3 program to implement
# the above approach
  
# Function to insert vertices
# to adjacency list
def insert(adj, u, v):
 
    # Insert a vertex v to vertex u
    adj[u].append(v)
    return
 
# Function to display adjacency list
def printList(adj, V):
     
    for i in range(V):
        print(i, end = '')
         
        for j in adj[i]:
            print(' --> ' + str(j), end = '')
             
        print()
         
    print()
         
# Function to convert adjacency
# list to adjacency matrix
def convert(adj, V):
 
    # Initialize a matrix
    matrix = [[0 for j in range(V)]
                 for i in range(V)]
     
    for i in range(V):
        for j in adj[i]:
            matrix[i][j] = 1
     
    return matrix
  
# Function to display adjacency matrix
def printMatrix(adj, V):
     
    for i in range(V):
        for j in range(V):
            print(adj[i][j], end = ' ')
             
        print()
         
    print()
         
# Driver code
if __name__=='__main__':
 
    V = 5
  
    adjList = [[] for i in range(V)]
  
    # Inserting edges
    insert(adjList, 0, 1)
    insert(adjList, 0, 4)
    insert(adjList, 1, 0)
    insert(adjList, 1, 2)
    insert(adjList, 1, 3)
    insert(adjList, 1, 4)
    insert(adjList, 2, 1)
    insert(adjList, 2, 3)
    insert(adjList, 3, 1)
    insert(adjList, 3, 2)
    insert(adjList, 3, 4)
    insert(adjList, 4, 0)
    insert(adjList, 4, 1)
    insert(adjList, 4, 3)
  
    # Display adjacency list
    print("Adjacency List: ")
    printList(adjList, V)
  
    # Function call which returns
    # adjacency matrix after conversion
    adjMatrix = convert(adjList, V)
  
    # Display adjacency matrix
    print("Adjacency Matrix: ")
    printMatrix(adjMatrix, V)
  
# This code is contributed by rutvik_56


C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to insert vertices to adjacency list
static void insert(List []adj,
                        int u, int v)
{
     
    // Insert a vertex v to vertex u
    adj[u].Add(v);
    return;
}
 
// Function to display adjacency list
static void printList(List []adj,
                           int V)
{
    for(int i = 0; i < V; i++)
    {
        Console.Write(i);
 
        foreach(int j in adj[i])
            Console.Write(" --> " + j);
             
        Console.WriteLine();
    }
    Console.WriteLine();
}
 
// Function to convert adjacency
// list to adjacency matrix
static int[,] convert(List []adj,
                           int V)
{
     
    // Initialize a matrix
    int [,]matrix = new int[V, V];
 
    for(int i = 0; i < V; i++)
    {
        foreach(int j in adj[i])
            matrix[i, j] = 1;
    }
    return matrix;
}
 
// Function to display adjacency matrix
static void printMatrix(int[,] adj, int V)
{
    for(int i = 0; i < V; i++)
    {
        for(int j = 0; j < V; j++)
        {
            Console.Write(adj[i, j] + " ");
        }
        Console.WriteLine();
    }
    Console.WriteLine();
}
 
// Driver code
public static void Main(String[] args)
{
    int V = 5;
 
    List []adjList = new List[V];
    for(int i = 0; i < adjList.Length; i++)
        adjList[i] = new List();
         
    // Inserting edges
    insert(adjList, 0, 1);
    insert(adjList, 0, 4);
    insert(adjList, 1, 0);
    insert(adjList, 1, 2);
    insert(adjList, 1, 3);
    insert(adjList, 1, 4);
    insert(adjList, 2, 1);
    insert(adjList, 2, 3);
    insert(adjList, 3, 1);
    insert(adjList, 3, 2);
    insert(adjList, 3, 4);
    insert(adjList, 4, 0);
    insert(adjList, 4, 1);
    insert(adjList, 4, 3);
 
    // Display adjacency list
    Console.Write("Adjacency List: \n");
    printList(adjList, V);
 
    // Function call which returns
    // adjacency matrix after conversion
    int[,] adjMatrix = convert(adjList, V);
 
    // Display adjacency matrix
    Console.Write("Adjacency Matrix: \n");
    printMatrix(adjMatrix, V);
}
}
 
// This code is contributed by amal kumar choubey


输出:
Adjacency List: 
0 --> 1 --> 4
1 --> 0 --> 2 --> 3 --> 4
2 --> 1 --> 3
3 --> 1 --> 2 --> 4
4 --> 0 --> 1 --> 3

Adjacency Matrix: 
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

时间复杂度: O(N*M)
辅助空间: O(N 2 )

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live