Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5962239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:04:40+00:00 2026-05-22T19:04:40+00:00

I want to put in a adjacency matrix the following graph: The left figure

  • 0

I want to put in a adjacency matrix the following graph:
enter image description here

The left figure is a node link representation of an 8 node network. The right one is an adjacency matrix representation of the same network. The number of
grey cells is equal to the number of links in the graph.

So I want to make a static assigned adjacency matrix. What would be the best approach in C language?

I was thinking something like:

int Matrix[8][8];

and then assign a 1 if the node is connected to other node and 0 if it is not. Also I was thinking to keep a counter for the number of neighbours a node has, like A has 2, B has 3…

But all this I want to be static not dynamic.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:04:41+00:00Added an answer on May 22, 2026 at 7:04 pm

    In C99, use enum and ‘designated initializers’:

    enum { A, B, C, D, E, F, G, H };
    
    static const int Matrix[8][8] =
    {
        [A] = { [B] = 1, [C] = 1 },
        [B] = { [A] = 1, [C] = 1, [D] = 1 },
        [C] = { [B] = 1, [F] = 1 },
        [D] = { [H] = 1 },
        [E] = { [D] = 1, [F] = 1, [G] = 1 },
        [F] = { [E] = 1, [G] = 1 },
        [G] = { [F] = 1, [H] = 1 },
        [H] = { [E] = 1, [G] = 1 },
    };
    

    This is a direct transcription of the table you provide; I’ve not verified the table against the graph. The elements without an explicit initializer are zeroed, of course. You could decide to align the close braces, though it isn’t necessary; I quite possibly would.

    If you don’t have C99 support available, you are left with manually populating the 2D array:

    static const int Matrix[8][8] =
    {  /* A  B  C  D  E  F  G  H */
        { 0, 1, 1, 0, 0, 0, 0, 0 }, /* A */
        { 1, 0, 1, 1, 0, 0, 0, 0 }, /* B */
        { 0, 1, 0, 0, 0, 1, 0, 0 }, /* C */
        { 0, 0, 0, 0, 0, 0, 0, 1 }, /* D */
        { 0, 0, 0, 1, 0, 1, 1, 0 }, /* E */
        { 0, 0, 0, 0, 1, 0, 1, 0 }, /* F */
        { 0, 0, 0, 0, 0, 1, 0, 1 }, /* G */
        { 0, 0, 0, 0, 1, 0, 1, 0 }, /* H */
    };
    

    If you have the graphs presented in some text form, you could write a Perl, Python, or Awk script (to name but three suitable languages) to generate the output for you automatically.


    Note: if you want to keep a counter of the number of neighbours that an element has (meaning, I assume, the number of neighbours that can be reached from this node, rather than the number of neighbours that can reach this node – or arrows out rather than arrows in), then you need a more complex structure than a simple 2D array. You’d probably use an array of structures:

    enum { A, B, C, D, E, F, G, H };
    enum { MAX_GRAPH_SIZE = 8 };
    
    typedef struct Node
    {
        unsigned char n_out;
        unsigned char nodes[MAX_GRAPH_SIZE];
    } Node;
    
    static const Node Matrix[MAX_GRAPH_SIZE] =
    {
        [A] = { .n_out = 2, .nodes = { [B] = 1, [C] = 1          } },
        [B] = { .n_out = 3, .nodes = { [A] = 1, [C] = 1, [D] = 1 } },
        [C] = { .n_out = 2, .nodes = { [B] = 1, [F] = 1          } },
        [D] = { .n_out = 1, .nodes = { [H] = 1                   } },
        [E] = { .n_out = 3, .nodes = { [D] = 1, [F] = 1, [G] = 1 } },
        [F] = { .n_out = 2, .nodes = { [E] = 1, [G] = 1          } },
        [G] = { .n_out = 2, .nodes = { [F] = 1, [H] = 1          } },
        [H] = { .n_out = 2, .nodes = { [E] = 1, [G] = 1          } },
    };
    

    I’m not at all convinced that the savings compared with computing the number of arrows out of (or into) a node warrants the extra complexity. Notationally, when referencing elements of the array, you’d now have to write:

    Matrix[i].nodes[j]
    

    instead of the simpler:

    Matrix[i][j]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want put one TextView at left upper corner(0,0), one TextView at right upper
I want put following check mark in side right tag <li> . How is
I want put this two value together (Side by side), as following image, but
I have cross-tab and i want put second cross-tab to right side, but i
I want to put label on any node, I plot nodes with its edges
I have two datasets named dataset1,dataset2. I want put the left outer join relation
I want put all configuration data from WPF projects to one file for whole
I want put content li in 2 column and want use of float:right; in
I want put a button on android edittextpreference. I create a custom editextpreference: public
I've projected an Intranet Ajax application and I want put it in a full

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.