-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph
More file actions
123 lines (120 loc) · 2.57 KB
/
Copy pathgraph
File metadata and controls
123 lines (120 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
void matrix(int **,int);
int main()
{
int n,e,i,**w,j;
int r1,c1,w1;
char ch[5];
cout<<"Enter number of nodes and edges\n";
cin>>n;
cin>>e;
w=(int**)malloc(n*sizeof(int*));
for(i=0;i<n;i++)
{
*(w+i)=(int *)calloc(n,sizeof(int));
}
cout<<"Is the graph directed"<<endl;
cin>>ch;
cout<<"Enter the start node, end node and weight of edge "<<endl;
for(i=0;i<e;i++) {
cin>>r1>>c1>>w1;
w[r1][c1] = w1;
if(strcmp(ch,"no")==0) {
w[c1][r1] = w1;
}
}
cout<<"Adjacency Matrix Representation:\n";
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
cout<<w[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
//Graph --- Adjacency matrix and Adjacency list representation
#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
struct node
{
int nodeno;
int edgeweight;
struct node *link;
};
struct node * createNode(int nodeno, int w) {
struct node *p = (struct node *) malloc(sizeof(struct node) );
p->nodeno = nodeno;
p->edgeweight = w;
p->link = NULL;
return p;
}
struct node convmattolist(int A,int n,char *dir)
{
struct node list = (struct node ) malloc(n * sizeof(struct node*));
int i,j;
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
if( A[i][j] > 0 ) {
if(*(list+i) == NULL) {
*(list+i) = createNode(j,*(*(A+i)+j));
}
else {
struct node *start = *(list+i);
while(start->link != NULL) {
start = start->link;
}
start->link = createNode(j,*(*(A+i)+j));
}
}
}
}
return list;
}
void printlist(struct node **list,int n)
{
cout<<"Adjacency List Representation:\n";
int i;
for(i=0;i<n;i++) {
struct node *s = *(list+i);
if(s != NULL) {
cout<<"Node "<<i<<" is connected :\n",i;
while(s!=NULL) {
cout<<"Node "<<s->nodeno<<" edge weight "<<s->edgeweight<<endl;
s = s->link;
}
}
}
}
int main()
{
int n,e,i,**w,j;
int r1,c1,w1;
char ch[5];
struct node **list;
cout<<"Enter number of nodes and edges\n";
cin>>n;
cin>>e;
w=(int**)malloc(n*sizeof(int*));
for(i=0;i<n;i++)
{
*(w+i)=(int *)calloc(n,sizeof(int));
}
cout<<"Is the graph directed"<<endl;
cin>>ch;
cout<<"Enter the start node, end node and weight of edge "<<endl;
for(i=0;i<e;i++) {
cin>>r1>>c1>>w1;
w[r1][c1] = w1;
if(strcmp(ch,"no")==0) {
w[c1][r1] = w1;
}
}
list = convmattolist(w,n,ch);
printlist(list,n);
return 0;
}