Hello, I’ve got an assignment from my math teacher to do with c++, because I am studying IT. The problem is that we didn’t even learn c++, I know only what I thought myself, so I need someone’s help. The task is to make a simple program that would be able to find an answer for greedy coloring method. I have a graph G, the answer should look something like this: 1, 3, 5, 6 belong to color 3; 4, 8 belong to color 2; 2, 6 belong to color 1. http://imgur.com/3zesQo1 Can someone help me with this? I found some source code on the internet but I need much more simple code, so I would be able to explain what is happening in the program.

Graph: http://imgur.com/jddZQi5

//So far I got this:
#include <iostream>
#include <conio.h>

const int R = 8;
const int C = 4;

int safe(int a, int G[R][C], int color[], int c);
int checking(int G[R][C], int m, int color[], int a);
void graphcoloring(int G[R][C], int m);
void coloring(int color[]);

using namespace std;

int main()
{
    int G[R][C] = {
            { 2, 8, 0, 0 },
            { 1, 3, 4, 8 },
            { 2, 4, 0, 0 },
            { 2, 3, 5, 6 },
            { 4, 6, 0, 0 },
            { 4, 5, 7, 0 },
            { 6, 8, 0, 0 },
            { 1, 2, 7, 0 } };
    int m = 3; //colors
    graphcoloring(G, m);
    _getch();
}
int safe(int a, int G[R][C], int color[], int c)
{
    for (int i = 0; i < R; i++)
    {
        if (G[R][i] && c == color[i])
            return 0;
        else
            return 1;
    }
}
int checking(int G[R][C], int m, int color[], int a)
{
    int c;
    if (a == R)
        return 1;
    for (c = 1; c <= m; c++)
    {
        if (safe(a, G, color, c))
        {
            color[a] = c;
            if (checking(G, m, color, a + 1) == 1)
                return 1;
            else
                color[a] = 0;
        }
    }
}
void graphcoloring(int G[R][C], int m)
{
    int color[R];
    for (int i = 0; i < R; i++)
        color[i] = 0;
    if (checking(G, m, color, 0) == 0)
    {
        cout << "Can't color";
        return;
    }
    coloring(color);
}
void coloring(int color[])
{
    cout << "The colors are: " << endl;
    for (int i = 0; i < R; i++)
        cout << color[i] << endl;
}

That looks like code you found on the web. I think it's also at http://cboard.cprogramming.com/cplusplus-programming/170525-help-would-appreciated.html so found code may not help you get to your goal.

Think over your problem and break it down to steps you can write in code. Even a beginner coder has to break it down to such steps and if a step can't be coded then you break that step down further.

Lifting code off the web may only lead to you being more confused as you don't know what that code does.

This article has been dead for over six months. Start a new discussion instead.