Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the program of Adjacency Matrix #1077

Merged
merged 2 commits into from
Nov 5, 2024
Merged

Conversation

shuvojitss
Copy link
Contributor

@shuvojitss shuvojitss commented Nov 4, 2024

Pull Request for PyVerse 💡

Requesting to submit a pull request to the PyVerse repository.


Issue Title

Please enter the title of the issue related to your pull request.
Adjacency Matrix

  • I have provided the issue title.

Info about the Related Issue

What's the goal of the project?
Write a program in python to create adjacency matrix of a given graph. If a graph has n vertices, we use n x n matrix to represent the graph.Let's assume the n x n matrix as adj[n][n].

  • if there is an edge from vertex i to j, mark adj[i][j] as 1. i.e. adj[i][j] == 1

  • if there is no edge from vertex i to j, mark adj[i][j] as 0. i.e. adj[i][j] == 0

  • I have described the aim of the project.


Name

Please mention your name.
Shuvojit Samanta

  • I have provided my name.

GitHub ID

Please mention your GitHub ID.
shuvojitss

  • I have provided my GitHub ID.

Email ID

Please mention your email ID for further communication.
[email protected]

  • I have provided my email ID.

Identify Yourself

Mention in which program you are contributing (e.g., WoB, GSSOC, SSOC, SWOC).
GSSOC

  • I have mentioned my participant role.

Closes

Enter the issue number that will be closed through this PR.
Closes: #1068

  • I have provided the issue number.

Describe the Add-ons or Changes You've Made

Give a clear description of what you have added or modified.

Algorithm for Graph Representation using an Adjacency Matrix

  1. Initialize the Graph

    • Create a 2D list (adjacency matrix) adj with dimensions 10 x 10, initialized to 0.
    • Accept the number of nodes no_of_nodes as input from the user.
  2. Input Neighbors and Populate the Matrix

    • For each node, prompt the user to enter the number of neighbors for that node.
    • Set adj[i][j] = 0 for all nodes initially.
    • For each neighbor of a node, set adj[i][neighbor] = 1 to establish a connection.
  3. Display the Adjacency Matrix

    • Print a header row with node identifiers (e.g., v1, v2, etc.).
    • For each node, print the row of the adjacency matrix, representing the connections with other nodes.
  4. Main Execution

    • Call create_graph to build the adjacency matrix based on user input.
    • Call display_graph to display the adjacency matrix in a formatted manner.
  • I have described my changes.

Type of Change

Select the type of change:

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Code style update (formatting, local variables)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Describe how your changes have been tested.
I have tested it in all python editors and its working. I am providing a sample input output to describe it.

Sample Input:

Enter the number of nodes in G: 5

Enter the number of neighbors of 0: 2
Enter the neighbors of 0 (0-based indices): 1 2

Enter the number of neighbors of 1: 2
Enter the neighbors of 1 (0-based indices): 0 3

Enter the number of neighbors of 2: 2
Enter the neighbors of 2 (0-based indices): 0 3

Enter the number of neighbors of 3: 3
Enter the neighbors of 3 (0-based indices): 2 1 4

Enter the number of neighbors of 4: 1
Enter the neighbors of 4 (0-based indices): 3

   0 ---- 1
   |      |
   2 ---- 1 --- 4                            

Sample Output:

The graph is:

The adjacency matrix is:

	      v1	v2	v3	v4	v5	
      v1	0	1	1	0	0	
      v2	1	0	0	1	0	
      v3	1	0	0	1	0	
      v4	0	1	1	0	1	
      v5	0	0	0	1	0
  • I have described my testing process.

Checklist

Please confirm the following:

  • My code follows the guidelines of this project.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly wherever it was hard to understand.
  • I have made corresponding changes to the documentation.
  • My changes generate no new warnings.
  • I have added things that prove my fix is effective or that my feature works.
  • Any dependent changes have been merged and published in downstream modules.

Copy link

github-actions bot commented Nov 4, 2024

👋 Thank you for opening this pull request! We're excited to review your contribution. Please give us a moment, and we'll get back to you shortly!

Feel free to join our community on Discord to discuss more!

@UTSAVS26 UTSAVS26 added Contributor Denotes issues or PRs submitted by contributors to acknowledge their participation. Status: Approved ✔️ PRs that have passed review and are approved for merging. level1 gssoc-ext labels Nov 5, 2024
@UTSAVS26 UTSAVS26 merged commit a30da6e into UTSAVS26:main Nov 5, 2024
1 of 23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Contributor Denotes issues or PRs submitted by contributors to acknowledge their participation. gssoc-ext level1 Status: Approved ✔️ PRs that have passed review and are approved for merging.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Code Addition Request]: Adjacency Matrix
2 participants