Stable marriage algorithm

Asked by At

Graph Algorithms: Stable marriage algorithm - generating "stable" relationship on a basis of basic data, concerning people (first name, last name, age) and lists of preferences

I've started to write this program, and I wanted to write it with help of weighted direct graph, and I have a problem with printing the data about people, and I have no idea how to fill a list with it. I do not ask to write me a code, just please give me some idea this is my code:

#include "pch.h"
#include <iostream>
#include <string>
#define N 6

using namespace std;

struct Graph {      
    struct Node* head[N];
};

struct Node {
    int age;
    string frst_nm;
    string lst_nm;
    int dest, weight;
    struct Node* next;
};

struct Edge {
    int src, dest, weight;

};

struct Inform
{
    int age;
    string first_name;
    string last_name;
};

struct Graph* createGraph(struct Edge edges[], struct Inform inf[], int n)
{
    unsigned i;
    int ag;
    string fr;
    string lst;

    struct Graph* graph = new Graph;

    /
    for (i = 0; i < N; i++)
        graph->head[i] = NULL;


    for (i = 0; i < n; i++) {

        int src = edges[i].src;
        int dest = edges[i].dest;
        int weight = edges[i].weight;
        int ag = inf[i].age;
        string fr = inf[i].first_name;
        string lst = inf[i].last_name; 

        struct Node* newNode = new Node;

        newNode->dest = dest;
        newNode->weight = weight;
        newNode->age = ag;
        newNode->frst_nm = fr;
        newNode->lst_nm = lst;

        newNode->next = graph->head[src];

        graph->head[src] = newNode;
    }

    return graph;
}

void printGraph(struct Graph* graph) {
    int i;
    for (i = 0; i < N; i++) {

        struct Node* ptr = graph->head[i];
        while (ptr != NULL) {
            cout << i << " : " << " " << ptr->age << " " << ptr->frst_nm << "  " << ptr->lst_nm << "   ";
            ptr = ptr->next;
        }

        cout << "\n";
    }
}

int main(void) {

    struct Edge edges[] = {
        { 0, 3, 3},{0,4,2}, { 0, 5, 1 }, { 1, 3, 1 }, { 1, 4, 3 },
        { 1, 5, 2 }, { 2, 3, 2 }, { 2, 4, 1 }, {2,5,3},{3,0,3},{3,1,1},{3,2,2},{4,0,3},{4,1,2}, {4,2,1},{5,0,1},{5,1,3},{5,2,2}
    };

    struct Inform inf[] = {
        {36, "John", "Smith"}, {25, "Edgar", "Po"}, {42, "Jim", "Kerry"}, {26, "Elise", "Cooper"},
    {39, "Merry", "Winston"}, {31, "Jess", "Bridget"}
    };

    int n = sizeof(edges) / sizeof(edges[0]);           

    struct Graph *graph = createGraph(edges,inf, n);

    printGraph(graph);

    return 0;
}

0 Answers