Answers for "Adjacency List"

1

how to convert adjacency list to adjacency matrix

#Python:
def convert_to_matrix(graph):
    matrix = []
    for i in range(len(graph)): 
        matrix.append([0]*len(graph))
        for j in graph[i]:
            matrix[i][j] = 1
    return matrix
#the lst shows in a form of each index(each inner list) as a form of vertex,
#and each element in the inner list as the vertices that each vertex connected to.
lst = [[1,2,3,5,6],[0,3,6,7],[0,3],[0,1,2,4],[3,5,8],[0,4,8],[0,1],[1],[4,5]]
print(convert_to_matrix(lst))
Posted by: Guest on July-24-2020
0

Adjacency List

#include<iostream >
 #include < vector >

using namespace std;

vector <int> adj[10];

int main()
{
    int x, y, nodes, edges;
    cin >> nodes;       // Number of nodes
    cin >> edges;       // Number of edges
    for(int i = 0;i < edges;++i)
    {
            cin >> x >> y;
        adj[x].push_back(y);        // Insert y in adjacency list of x
     }
for(int i = 1;i <= nodes;++i)
{   
        cout << "Adjacency list of node " << i << ": ";
    for(int j = 0;j < adj[i].size();++j)
        {
        if(j == adj[i].size() - 1)
                cout << adj[i][j] << endl;
        else
            cout << adj[i][j] << " --> ";
}
}
return 0;
}
Posted by: Guest on October-09-2021

Code answers related to "Adjacency List"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language