The problem analytically: There is a single (wx)Button. When it is pressed, some of my routines may be run, some others not, but in the end the values of the two rich text controls are set to one of my functions each.
The program is about saving edges of a graph in a 2-D sparse array (linked list), creating a spanning tree on it, and finding cycles. Being 2D and double-link-listed, the members of the sparse array contain pointers to the previous, next, up and down elements. Also, the edges they represent could or could not be part of the spanning tree (STE=spanning tree edge). This is what const char* print_all_edges() is about, and sets WxRichTextCtrl_sparse->SetValue(print_all_edges()). Then, the program searches for cycles and reports them. This is what const char* const char* print_all_cycles(void) is about, and sets
WxRichTextCtrl_cycles->SetValue(print_all_cycles());
(Normal) Output after adding edges (1,0),(1,2),(2,0): (Note that the insert function inserts the symmetricals too, this is a bidirectional graph):
WxRichTextCtrl_sparse:
Edge (1,0):STE=1. P(x-): None N(x+): (2,0) U(y-): None D(y+): (1,2)
Edge (2,0):STE=1. P(x-): (1,0) N(x+): None U(y-): None D(y+): (2,1)
Edge (0,1):STE=1. P(x-): None N(x+): (2,1) U(y-): None D(y+): (0,2)
Edge (2,1):STE=0. P(x-): (0,1) N(x+): None U(y-): (2,0) D(y+): None
Edge (0,2):STE=1. P(x-): None N(x+): (1,2) U(y-): (0,1) D(y+): None
Edge (1,2):STE=0. P(x-): (0,2) N(x+): None U(y-): (1,0) D(y+): None
WxRichTextCtrl_cycles:
Graph cycle for edge (2,1):(spanning tree root: 0)
(0,2)->(2,1)<-(1,0)
Graph cycle for edge (1,2):
(0,1)->(1,2)<-(2,0)
Now, also consider the normal output after just inputting edge (1,0):
WxRichTextCtrl_sparse:
Edge (1,0):STE=1. P(x-): None N(x+): None U(y-): None D(y+): None
Edge (0,1):STE=1. P(x-): None N(x+): None U(y-): None D(y+): None
WxRichTextCtrl_cycles:
No cycles detected: Graph is a tree.
...and here''s the output, after just adding (1,0), that I get through running the program outside the IDE:
WxRichTextCtrl_sparse:
Edge (1,0):STE=1. P(x-): None N(x+): None U
WxRichTextCtrl_cycles:
No cycles detected: Graph is a tree.
This is the first and only time that the first routine "works a bit", then, any output on both windows is nothing, emptiness.
Here's some of the the code:
Code: Select all
void Sparse_Array_GUIDlg::WxButton1Click(wxCommandEvent& event)
{
switch(WxRadioBox1->GetSelection())
{
case 1://Delete
delete_edge(find_edge(WxSpinCtrl1->GetValue(),WxSpinCtrl2->GetValue()));
break;
case 0://Insert
insert_edge(WxSpinCtrl1->GetValue(),WxSpinCtrl2->GetValue());
break;
}
WxSpinCtrl_root->SetValue(validroot(WxSpinCtrl_root->GetValue()));//set root to a valid one
create_spanning_tree(WxSpinCtrl_root->GetValue());
create_all_cycles();
WxRichTextCtrl_sparse->SetValue(print_all_edges());
WxRichTextCtrl_cycles->SetValue(print_all_cycles());
}
Code: Select all
typedef struct pathmember * pp;
typedef struct edge * ep;//Edge Pointer
struct edge{
int x,y;
bool STE;
pp p1,p2;
ep prev,next,up,down;
};
struct nodetype{
char STN;//(0:not STN)(1:STN, but (possibly) has neighbours that are not STN)(2:STN + neighbours=STN)
//1 are nodes of current spanning tree level search. 2 are old, 0 are future.
bool TN;//if false, this node doesn't exist
int parent;
};
struct pathmember{
pathmember * next;
ep target;
};
Code: Select all
const char* print_edge(ep temp, bool longmode)
{
stringstream s;
s.str("");//Initialization of s
unsigned char x=2;
if(longmode) s<<"Edge ";
s<<"("<<temp->x<<","<<temp->y<<")";
if(longmode) s<<":STE="<<temp->STE;
if(longmode)
{
s<<". P(x-): ";
if(temp->prev) s<<"("<<temp->prev->x<<","<<temp->y<<")"; else s<<"None";
s<<" N(x+): ";
if(temp->next) s<<"("<<temp->next->x<<","<<temp->y<<")"; else s<<"None";
s<<" U(y-): ";
if(temp->up) s<<"("<<temp->x<<","<<temp->up ->y<<")"; else s<<"None";
s<<" D(y+): ";
if(temp->down) s<<"("<<temp->x<<","<<temp->down->y<<")"; else s<<"None";
s<<"\n";
}
return s.str().c_str();
}
Code: Select all
const char* print_all_edges()
{
int i,j;
string s="";
ep temp;
for(i=0;i<30;i++)
{
temp=SparseArrayVertical[i];
while(temp)
{
s+=print_edge(temp);
temp=temp->next;
}
}
if(s=="") return "Sparse Array is empty";
return s.c_str();
}
Code: Select all
const char* print_cycle(ep i)
{
string s="Graph cycle for edge ";
s+=print_edge(i,false);
s+=":\n";
//s+="\nPath 1/2: From joint to sentinel (forward):\n";
do
{
s+=print_edge(i->p1->target,false);
s+="->";
i->p1=i->p1->next;
}while(i->p1->next);
s+=print_edge(i,false);
//s+="Path 2/2: From joint to sentinel (backward):\n";
/*do
{
s+="<-";
s+=print_edge(i->p2->target,false);
i->p2=i->p2->next;
}while(i->p2->next);*/
s+=reverse_path_print(i->p2);
s+="\n\n";
return s.c_str();
}
Code: Select all
const char* print_all_cycles(void)
{
int i,j;
string s="";
ep temp;
for(i=0;i<30;i++)
{
temp=SparseArrayVertical[i];
while(temp)
{
if(temp->p1)
{
cout<<"Called: printcycle for edge: "<<print_edge(temp,false);
s+=print_cycle(temp);
}
temp=temp->next;
}
}
if(s=="") return "No cycles detected: Graph is a tree.";
return s.c_str();
}