如何在无权图C程序中删除两个节点之间的边

如何在无权图C程序中删除两个节点之间的边,c,data-structures,struct,heap,dijkstra,C,Data Structures,Struct,Heap,Dijkstra,我正在用Dijkstra的算法挑战我的技能,并创建一个最多可以有500个朋友的社交网络范例 在我的代码中有三个函数: AddFriendly(在朋友节点之间建立一条边) WantToBeFriend(使用Dijkstra算法通过最短路径建议朋友) RemoveFriends(删除朋友节点之间的边) 在广泛研究和开发的帮助下,我实现了Dijkstra算法。通过调用addFriendly函数,我可以简单地添加节点及其边(权重始终为1)。通过使用最短路径和调用WantToBeFriend功能,好友

我正在用Dijkstra的算法挑战我的技能,并创建一个最多可以有500个朋友的社交网络范例

在我的代码中有三个函数:

  • AddFriendly(在朋友节点之间建立一条边)
  • WantToBeFriend(使用Dijkstra算法通过最短路径建议朋友)
  • RemoveFriends(删除朋友节点之间的边)
在广泛研究和开发的帮助下,我实现了Dijkstra算法。通过调用addFriendly函数,我可以简单地添加节点及其边(权重始终为1)。通过使用最短路径和调用WantToBeFriend功能,好友建议也能很好地发挥作用

但我无法实现删除友谊功能。此函数的目的是简单地删除两个节点(朋友)之间的边。如果两个用户不是朋友,则应打印错误“无友谊”

以下是我迄今为止的代码工作:

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>

#define NAME_LENGTH 20
#define FRIENDS 500

typedef struct {
    int vertex;
    int weight;
} edge_t;

typedef struct {
    edge_t **edges;
    int edges_len;
    int edges_size;
    int dist;
    int prev;
    int visited;
} vertex_t;

typedef struct {
    vertex_t **vertices;
    int vertices_len;
    int vertices_size;
} graph_t;

typedef struct {
    int *data;
    int *prio;
    int *index;
    int len;
    int size;
} heap_t;

struct DList{
    char name[NAME_LENGTH + 1];
    int id;
    int friends[FRIENDS];
    struct DList *next;
};
struct DList *head = NULL;

int SIZE = -1;



int addinDList(char *name){
    struct DList *new_data = (struct DList*) malloc(sizeof(struct DList));
    SIZE++;
    new_data->id = SIZE;
    strcpy(new_data->name , name);
    int i=0;
    for(i; i<FRIENDS; i++){
        new_data->friends[i] = -1;
    }
    new_data->next = NULL;
    if(head == NULL){
        head = new_data;
        return new_data->id;
    }else{
        struct DList *ptr = head;
        while(ptr->next != NULL){
            ptr = ptr->next;
        }
        ptr->next = new_data;
        return new_data->id;
    }
}

int searchInDList(char *name){
    if (head == NULL) return 0;
    struct DList *ptr = head;
    while(ptr != NULL){
        if(strcmp(ptr->name, name) == 0)
            return 1;
        else
            ptr = ptr->next;
    }
    return 0;
}

int getDListID(char *name){
    if (head == NULL) return 0;
    struct DList *ptr = head;
    while(ptr != NULL){
        if(strcmp(ptr->name, name) == 0)
            return ptr->id;
        else
            ptr = ptr->next;
    }
    return -1;  
}

char* getDListName(int id){
    if (head == NULL) return 0;
    struct DList *ptr = head;
    while(ptr != NULL){
        if(ptr->id == id)
            return ptr->name;
        else
            ptr = ptr->next;
    }       
}

struct DList *get(int id){
    if (head == NULL) return 0;
    struct DList *ptr = head;
    while(ptr != NULL){
        if(ptr->id == id)
            return ptr;
        else
            ptr = ptr->next;
    }   
}

void printDList(){
    if(head == NULL) return;
    struct DList *ptr = head;
    while(ptr != NULL){
        printf("%s: %d\n", ptr->name, ptr->id);
        ptr = ptr->next;
    }
}

void add_vertex(graph_t *g, int i) {
    if (g->vertices_size < i + 1) {
        int size = g->vertices_size * 2 > i ? g->vertices_size * 2 : i + 4;
        g->vertices = realloc(g->vertices, size * sizeof (vertex_t *));
        int j;
        for (j = g->vertices_size; j < size; j++)
            g->vertices[j] = NULL;
        g->vertices_size = size;
    }
    if (!g->vertices[i]) {
        g->vertices[i] = calloc(1, sizeof (vertex_t));
        g->vertices_len++;
    }
}

int add_edge(graph_t *g, char *val1, char *val2, int a, int b) {
    int w=1;
    struct DList *ptr = get(a);
    int i=0;
    int check = 1;
    for(i=0; i<FRIENDS; i++){
        if(ptr->friends[i] == b){
            check = 0;
            return check;   // failed to add
        }
    }
    i = 0;
    while(ptr->friends[i] != -1){
        i++;
    }
    if(ptr->friends[i] == -1)
        ptr->friends[i] = b;
    add_vertex(g, a);
    add_vertex(g, b);
    vertex_t *v = g->vertices[a];
    if (v->edges_len >= v->edges_size) {
        v->edges_size = v->edges_size ? v->edges_size * 2 : 4;
        v->edges = realloc(v->edges, v->edges_size * sizeof (edge_t *));
    }
    edge_t *e = calloc(1, sizeof (edge_t));
    e->vertex = b;
    e->weight = w;
    v->edges[v->edges_len++] = e;
    return 1;   // success
}

heap_t *create_heap(int n) {
    heap_t *h = calloc(1, sizeof (heap_t));
    h->data = calloc(n + 1, sizeof (int));
    h->prio = calloc(n + 1, sizeof (int));
    h->index = calloc(n, sizeof (int));
    return h;
}

void push_heap(heap_t *h, int v, int p) {
    int i = h->index[v] == 0 ? ++h->len : h->index[v];
    int j = i / 2;
    while (i > 1) {
        if (h->prio[j] < p)
            break;
        h->data[i] = h->data[j];
        h->prio[i] = h->prio[j];
        h->index[h->data[i]] = i;
        i = j;
        j = j / 2;
    }
    h->data[i] = v;
    h->prio[i] = p;
    h->index[v] = i;
}

int min(heap_t *h, int i, int j, int k) {
    int m = i;
    if (j <= h->len && h->prio[j] < h->prio[m])
        m = j;
   if (k <= h->len && h->prio[k] < h->prio[m])
        m = k;
    return m;
}

int pop_heap (heap_t *h) {
    int v = h->data[1];
    int i = 1;
    while (1) {
        int j = min(h, h->len, 2 * i, 2 * i + 1);
        if (j == h->len)
            break;
        h->data[i] = h->data[j];
        h->prio[i] = h->prio[j];
        h->index[h->data[i]] = i;
        i = j;
    }
    h->data[i] = h->data[h->len];
    h->prio[i] = h->prio[h->len];
    h->index[h->data[i]] = i;
    h->len--;
    return v;
}

void Dijkstra(graph_t *g, char* val1, char* val2) {
    int a, b, i, j;
    a = getDListID(val1);
    b = getDListID(val2);
    if (a == -1 || b == -1){
        printf("Wrong Arguments\n");
    }
    for (i = 0; i < g->vertices_len; i++) {
        vertex_t *v = g->vertices[i];
        v->dist = INT_MAX;
        v->prev = 0;
        v->visited = 0;
    }
    vertex_t *v = g->vertices[a];
    v->dist = 0;
    heap_t *h = create_heap(g->vertices_len);
    push_heap(h, a, v->dist);
    while (h->len) {
        i = pop_heap(h);
        if (i == b)
            break;
        v = g->vertices[i];
        v->visited = 1;
        for (j = 0; j < v->edges_len; j++) {
            edge_t *e = v->edges[j];
            vertex_t *u = g->vertices[e->vertex];
            if (!u->visited && v->dist + e->weight <= u->dist) {
                u->prev = i;
                u->dist = v->dist + e->weight;
                push_heap(h, e->vertex, u->dist);
            }
        }
    }
}


void ShortestPath(graph_t *g, char* val1) {
    int i, n, j;
    vertex_t *v, *u;
    i = getDListID(val1);
    v = g->vertices[i];
    if (v->dist == INT_MAX) {
        printf("\nSorry, none of Your friends can help introduce you to %s\n", val1);
        return;
    }
    for (n = 1, u = v; u->dist; u = g->vertices[u->prev], n++)
    ;
    int *path = calloc(n, sizeof(int));
    for (j = 0, u = v; u->dist; u = g->vertices[u->prev], j++){
        path[n - j - 2] = u->prev;
    }
    path[n - 1] = i;
    if (v->dist>1){
        printf("\n- Length of the shortest path: %d\n", v->dist);
        if (v->dist == 2)
            printf("- Your mutual friend is %s\n", getDListName(path[1]));
        else if(v->dist > 2){
            printf("- Your intermediate friend is %s\n", getDListName(path[1]));
        }
        printf("- Path: ");
        int loop = 0;
        for(loop; loop <= n -1 ; loop++){
            printf("%s ", getDListName(path[loop]));
        }
        printf("\n");   
    } else if (v->dist == 1){
        printf("AlreadyAFriendError\n");
    }
    free(path);
}

void WantToBeFriend(graph_t *g, char* command, char* friend1, char* friend2){
    printf("%s %s %s ", command, friend1, friend2);
    Dijkstra(g, friend1, friend2);
    ShortestPath(g, friend2);
}

void AddFriendship(graph_t *g, char* command, char* friend1, char* friend2){
    int add_check;
    printf("%s %s %s ", command, friend1, friend2);
    int a,b;
    if (searchInDList(friend1) == 0){
        a = addinDList(friend1);
    } else {
        a = getDListID(friend1);
    }
    if (searchInDList(friend2) == 0){
        b = addinDList(friend2);
    } else {
        b = getDListID(friend2);
    }
    add_check = add_edge(g, friend1, friend2, a, b);
    if (add_check == 0) {
        printf("ExistingFriendshipError\n");
        return;
    }
    add_check = add_edge(g, friend2, friend1, b, a);
    if (add_check == 0){
        printf("ExistingFriendshipError\n");
        return;     
    }
    printf("\n");
}

void RemoveFriendship(graph_t *g, char* command, char* friend1, char* friend2){
    /*


        I am struggling here...


    */
}

int main () {
    graph_t *g = calloc(1, sizeof (graph_t));
    AddFriendship(g, "AddFriendship", "Alice", "Bob"); 
    AddFriendship(g, "AddFriendship", "Bob", "Carol");
    AddFriendship(g, "AddFriendship", "Carol", "Alice");
    AddFriendship(g, "AddFriendship", "David", "Emily");
    AddFriendship(g, "AddFriendship", "Emily", "Frank");
    AddFriendship(g, "AddFriendship", "Frank", "George");
    AddFriendship(g, "AddFriendship", "George", "Emily");
    AddFriendship(g, "AddFriendship", "David", "Frank");
    AddFriendship(g, "AddFriendship", "Alice", "George");
    AddFriendship(g, "AddFriendship", "Alice", "David");

    AddFriendship(g, "AddFriendship", "Alice", "Emily");
    AddFriendship(g, "AddFriendship", "Carol", "George");
    RemoveFriendship(g, "RemoveFriendship", "Alice", "David");
    WantToBeFriend(g, "WantToBeFriend", "Alice", "Frank");

    return 0;
}
#包括
#包括
#包括
#包括
#定义名称\u长度20
#定义朋友500
类型定义结构{
int顶点;
整数权重;
}边缘;
类型定义结构{
边缘**边缘;
内部边缘;
整型边缘尺寸;
国际区;
国际通行证;
国际访问;
}顶点t;
类型定义结构{
顶点**顶点;
int顶点_len;
int-u大小;
}图t;
类型定义结构{
int*数据;
int*prio;
int*索引;
内伦;
整数大小;
}堆;
结构列表{
字符名称[名称长度+1];
int-id;
int友人[友人];
结构数据列表*下一步;
};
结构数据列表*head=NULL;
整数大小=-1;
int addinDList(字符*名称){
struct DList*new_data=(struct DList*)malloc(sizeof(struct DList));
大小++;
新建数据->id=大小;
strcpy(新数据->名称、名称);
int i=0;
对于(i;ifriends[i]=-1;
}
新建数据->下一步=空;
if(head==NULL){
head=新的_数据;
返回新的_数据->id;
}否则{
结构数据列表*ptr=头部;
while(ptr->next!=NULL){
ptr=ptr->next;
}
ptr->next=新数据;
返回新的_数据->id;
}
}
int searchInDList(字符*名称){
if(head==NULL)返回0;
结构数据列表*ptr=头部;
while(ptr!=NULL){
if(strcmp(ptr->name,name)==0)
返回1;
其他的
ptr=ptr->next;
}
返回0;
}
int getDListID(字符*名称){
if(head==NULL)返回0;
结构数据列表*ptr=头部;
while(ptr!=NULL){
if(strcmp(ptr->name,name)==0)
返回ptr->id;
其他的
ptr=ptr->next;
}
返回-1;
}
char*getDListName(int id){
if(head==NULL)返回0;
结构数据列表*ptr=头部;
while(ptr!=NULL){
如果(ptr->id==id)
返回ptr->name;
其他的
ptr=ptr->next;
}       
}
结构数据列表*获取(整数id){
if(head==NULL)返回0;
结构数据列表*ptr=头部;
while(ptr!=NULL){
如果(ptr->id==id)
返回ptr;
其他的
ptr=ptr->next;
}   
}
void printDList(){
if(head==NULL)返回;
结构数据列表*ptr=头部;
while(ptr!=NULL){
printf(“%s:%d\n”,ptr->name,ptr->id);
ptr=ptr->next;
}
}
void add_顶点(图_t*g,int i){
如果(g->U大小顶点大小*2>i?g->顶点大小*2:i+4;
g->Vertexts=realloc(g->Vertexts,size*sizeof(vertex_t*));
int j;
对于(j=g->顶点_size;j顶点[j]=NULL;
g->顶点大小=大小;
}
如果(!g->顶点[i]){
g->顶点[i]=calloc(1,sizeof(顶点_t));
g->顶点_len++;
}
}
int add_edge(图t*g,char*val1,char*val2,int a,int b){
int w=1;
结构数据列表*ptr=get(a);
int i=0;
整数检查=1;
对于(i=0;ifriends[i]==b){
检查=0;
返回检查;//添加失败
}
}
i=0;
while(ptr->friends[i]!=-1){
i++;
}
如果(ptr->friends[i]=-1)
ptr->friends[i]=b;
添加_顶点(g,a);
添加_顶点(g,b);
顶点_t*v=g->顶点[a];
如果(v->边缘大小>=v->边缘大小){
v->edges\u size=v->edges\u size?v->edges\u size*2:4;
v->edges=realloc(v->edges,v->edges\u size*sizeof(edge\u t*));
}
edge_t*e=calloc(1,sizeof(edge_t));
e->顶点=b;
e->重量=w;
v->edges[v->edges_len++]=e;
返回1;//成功
}
堆\u t*创建\u堆(int n){
heap_t*h=calloc(1,sizeof(heap_t));
h->data=calloc(n+1,sizeof(int));
h->prio=calloc(n+1,sizeof(int));
h->index=calloc(n,sizeof(int));
返回h;
}
无效推送堆(堆t*h,int v,int p){
inti=h->index[v]==0?++h->len:h->index[v];
int j=i/2;
而(i>1){
if(h->prio[j]data[i]=h->data[j];
h->prio[i]=h->prio[j];
h->索引[h->数据[i]]=i;
i=j;
j=j/2;
}
h->数据[i]=v;
h->prio[i]=p;
h->index[v]=i;
}
最小整数(堆,整数i,整数j,整数k){
int m=i;
if(j len&&h->prio[j]prio[m])
m=j;
if(k len&&h->prio[k]prio[m])
m=k;
返回m;
}
int pop_堆(heap_t*h){
int v=h->data[1];
int i=1;
而(1){
int j=min(h,h->len,2*i,2*i+1);
如果(j==h->len)
打破
h->data[i]=h->data[j];
h->prio[i]=h->prio[j];
h->索引[h->数据[i]]=i;
i=j;
}
h->data[i]=h->data[h->len];
h->prio[i]=h->prio[h->len];
h->索引[h->数据[i]]=i;
h->len--;
返回v;
}
void Dijkstra(图_t*g,char*val1,char*val2){
int a,b,i,j;
a=getDListID(val1);
b=getDListID(val2);
如果(a==-1 | | b==-1