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

  • Home
  • SEARCH
  • 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 354949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:58:20+00:00 2026-05-12T11:58:20+00:00

Just for kind of fun I’m developing almost every algorithm (if possible) shown in

  • 0

Just for kind of “fun” I’m developing almost every algorithm (if possible) shown in the book Introduction to Algorithms (Cormen) in C. I’ve reached the graphs chapters and I’m not sure how to design my functions, but first take a look at my data structures (hope this will make clear my question).

typedef struct s_multi_matrix
    {
        int                 width;
        int                 height;
        int                 depth;
        multi_matrix_type   type;   // stores either MMTYPE_INT or MMTYPE_RECORD enums to know where to read values (ivals or rvals)
        int *               ivals;
        DT_record *         rvals;
    } DT_multi_matrix;

typedef struct s_double_linked_list
    {
        DT_record   *   sentinel;
    } DT_double_linked_list;

typedef struct s_record // multi purpose structure
    {
        int key;
        enum e_record_type  type; // stores either TYPE_INT, TYPE_SZ, TYPE_FLOAT, TYPE_VOID to know how to use the data union
        union
        {
            int     ival;
            char *  sval;
            float   fval;
            void *  vval;
        } data;
        struct s_record * left, // for trees, disjoint sets and linked lists
                        * right,
                        * parent;
    } DT_record;


typedef struct s_graph  // this is the structure I'm focusing on right now
    {
        graph_type              type;       // GRAPH_DIRECTED or GRAPH_UNDIRECTED
        graph_adj_type          adj_type;   // ADJ_LIST or ADJ_MATRIX
        edge_type           etype;  // WEIGHTED or NOT_WEIGHTED
        union
        {
            DT_double_linked_list *     list;
            DT_multi_matrix *           matrix;
        } adjacency;
    } DT_graph;

So, I’m thinking about several functions to manipulate the DT_graph type:

// takes a pointer to a pointer to a graph, allocates memory and sets properties
void build_graph(DT_graph ** graph_ptr, graph_type gtype, graph_adj_type atype);
// prints the graph in file (using graphviz)
void print_graph(DT_graph * graph, char * graph_name);

This is the tricky part, since my graph type has several different type combinations (undirected and weighted edges, directed and weighted edges, undirected and not weighted edges, directed and not weighed edges, …) I’m wondering which is the best approach for the functions:

void dgraph_add_wedge(DT_graph * graph, DT_record * u, DT_record * v, int weight);
void ugraph_add_wedge(DT_graph * graph, DT_record * u, DT_record * v, int weight);
void dgraph_add_nwedge(DT_graph * graph, DT_record * u, DT_record * v);
void ugraph_add_nwedge(DT_graph * graph, DT_record * u, DT_record * v);

The first two would add a weighted vertex into a directed / undirected graph and the last two would do the same thing but without any weight related to the edge.
The other approach that comes to my mind looks like this:

void graph_add_edge(DT_graph * graph, DT_record * u, DT_record * v, edge_type etype, graph_type gtype);

This seems like a “golden” method for everything, and depending on the values for etype and gtype would do different operations on the graph.

Soooooooo, based upon your experiences and knowledge, what do you recommend?

BTW, I’m sure that any one has asked this before, since this is MY implementation.

  • 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-12T11:58:20+00:00Added an answer on May 12, 2026 at 11:58 am

    Pity this is plain C. With C++ several of these questions would be handled by the polymorphic features of the language. On the other hand studying algorithms that way make you focus on the algorithm/data-structure proper rather than some slick features of the language.

    Anyway… My two cents:

    With regards to selecting the d or u Graph Type: Why not addomg an attribute to the DT_Graph to inform the method(s) called of the graph type. After all, this is specified when the graph is created. ==> Bam! We’re down to only 2 methods.

    With regards to the edge weights… Maybe having two methods is preferable, API-wise : why bother the non weighed cases with the extra argument. Implementation-wise you can of course share as much of the logic as possible between all 4 cases. And frankly, once you’ve written all this you can still facade these behind a single “golden” method as the one you suggested.

    Good luck with your coding!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 201k
  • Answers 201k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Dialog controls are attached to the MFC object when subclassed,… May 12, 2026 at 8:08 pm
  • Editorial Team
    Editorial Team added an answer If it's using reflection, it's probably a really bad idea.… May 12, 2026 at 8:08 pm
  • Editorial Team
    Editorial Team added an answer You should be able to use touch, it fires callbacks… May 12, 2026 at 8:08 pm

Related Questions

Just for kind of fun I'm developing almost every algorithm (if possible) shown in
I'm making a small on-line clock (both for fun and to learn a bit
I was looking for a generic method in .Net to encode a string for
I am having trouble figuring out mem_fun_ref . I have to admit, I usually
I'm working through Real World Haskell , and at the moment doing the exercises

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.