Question

Request the user to determine the order (IV]) and size (El) of the graph Generate |El random ones into the adjacency matrix/list (Adj) to make a random undirected graph. (Make sure to have a symmetric matrix) Print the resulting adjacency matrix/list. 1. 2. 3.

I'm having trouble using the syntax using MATLAB and I need help for parts 1-3 in MATLAB code if anyone can help me thank you very much.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>

#include<malloc.h>

#include<string.h>

typedef struct adjacency{

int vname;

struct adjacency *adj;

} Adj;

typedef struct graph

{

int nvert;

int nedge;

int time;

int fi, st;

int *pi;

char *color;

Adj *base[10];

}G;

//display function

void display(G *g)

{

int i,j=0;

Adj *p;

for(i=0;i<g->nvert;i++)

{

p=g->base[i];

while(p){

printf("%d->",p->vname);

p=p->adj;

}

printf("\n");

}

}

// new graph allocation..............................

G* newgraph()

{

G *g;

Adj *nodes;

int i,k=0;

int u,v;

g=(G*)malloc(sizeof(G));

printf("Enter the number of vertices\n");

scanf("%d",&(g->nvert));

printf("Enter the Vertices\n");

g->pi=(int *)malloc(sizeof(int)*((g->nvert)+1));

g->fi=(int *)malloc(sizeof(int)*((g->nvert)+1));

g->st=(int *)malloc(sizeof(int)*((g->nvert)+1));

g->color=(char*)malloc(sizeof(char)*((g->nvert)+1));

for(i=0;i<g->nvert;i++)

{

g->pi[i]=-1;

g->color[i]='W';

g->base[i]=(Adj*)malloc(sizeof(Adj));

scanf("%d",&(g->base[i])->vname);

(g->base[i])->adj=NULL;

}

g->pi[i]=-1;

g->color[i]='W';

g->base[i]=NULL;

printf("Enter the number of edges in graph\n");

scanf("%d",&(g->nedge));

printf("Enter the Edges\n");

for(i=0;i<g->nedge;i++)

{

scanf("%d%d",&u,&v);

nodes=(Adj*)malloc(sizeof(Adj));

nodes->vname=u;

while(((g->base[k])->vname!=v)&&(g->base[k]))

k++;

if(g->base[k])

{

nodes->adj=(g->base[k])->adj;

(g->base[k])->adj=nodes;

k=0;

}

//second node....................

nodes=(Adj*)malloc(sizeof(Adj));

nodes->vname=v;

while(((g->base[k])->vname!=u)&&(g->base[k]))

k++;

if(g->base[k])

{

nodes->adj=(g->base[k])->adj;

(g->base[k])->adj=nodes;

k=0;

}

}

g->time=0;

return g;

}

// dfs visit...............

void dfs_visit(G g, Adj u)

{

Adj *v;

int i=0;

g->color[u->vname]='G';

g->time=g->time+1;

g->st[u->vname]=g->time;

v=u->adj;

while(v)

{

if(g->color[v->vname]=='W')

{

g->pi[v->vname]=u->vname;

while(((g->base[i])->vname)!=v->vname)

i++;

dfs_visit(g,g->base[i]);

}

v=v->adj;

}

g->color[u->vname]='B';

g->time=g->time+1;

g->fi[u->vname]=g->time;

}

//dfs of graph............

void dfs(G g, Adj s)

{

int i;

dfs_visit(g,s);

for(i=0;i<g->nvert;i++)

if((g->color[(g->base[i])->vname])=='W')

dfs_visit(g,g->base[i]);

}

int main()

{

G *g=newgraph();

int s;

printf("Enter the index source vertex....\n");

scanf("%d",&s);

display(g);

dfs(g,g->base[s]);

for(s=0;s<g->nvert;s++)

printf("%d\t%c\n",g->pi[(g->base[s])->vname],g->color[(g->base[s])->vname]);

printf("\n\n");

printf("\nStarting time finishiing time:\n");

for(s=0;s<g->nvert;s++)

printf("%d\t%d\n",g->st[(g->base[s])->vname],g->fi[(g->base[s])->vname]);

return 0;

}

CAUsers CS Nishtha DesktoplDSIndfs adjlist.exe ter the index source vertex.... arting time finishiing time: 10 ocess exited after 35.82 seconds with return value 0 ess any key to continue

#include<stdio.h>

#include<malloc.h>

typedef struct queue{

int data;

struct queue *link;

}Q;

void create(Q *front, Q *rear)

{

*front=*rear=NULL;

}

//////ENQUEUE

void enqueue(Q *front, Q *rear, int a)

{

Q *newn;

newn=(Q *)malloc(sizeof(Q)*1);

newn->data=a;

if(newn==NULL)

{

printf("Queue is full");

}

else{

if(*front==NULL)

{

*front=*rear=newn;

}

else{

(*rear)->link=newn;

*rear=newn;

}

}

}

////////////////////////////////////

int dequeue(Q *front, Q *rear)

{

int t;

Q *ptr;

if(*front==NULL)

{

printf("Q Is EMp\n");

return -1;

}

else

{

t=(*front)->data;

ptr=*front;

if(*front==*rear)

{

*front=*rear=NULL;

}

else

*front=(*front)->link;

free(ptr);

return t;

}

}

int main()

{

Q *first,*second;

int i,j,k,n,e,*visit,**a;

printf("\nEnter the number of vertices:");

scanf("%d",&n);

a=(int**)malloc(n*(sizeof(int*)));

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

a[i]=(int*)malloc(n*sizeof(int));

visit=(int*)malloc(n*sizeof(int));

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

for(j=0;j<n;j++)

a[i][j]=0;

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

visit[i]=0;

printf("\nEnter the number of Edges:");

scanf("%d",&e);

for(i=0;i<e;i++)

{

printf("Enter the %d Edge:",i+1);

scanf("%d%d",&j,&k);

a[j][k]=a[k][j]=1;

}

create(&first,&second);

enqueue(&first,&second,0);

while(first!=NULL)

{

i=dequeue(&first,&second);

printf("V%d\t",i);

visit[i]=1;

for(j=0;j<n;j++)

if((a[i][j]==1)&&(visit[j]==0))

{

enqueue(&first,&second,j);

visit[j]=1;

}

}

return 0;

}

Add a comment
Know the answer?
Add Answer to:
I'm having trouble using the syntax using MATLAB and I need help for parts 1-3 in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • answer 4, 5, 6, 7 using matlab Homework 10 1. Write the correct first-line syntax for...

    answer 4, 5, 6, 7 using matlab Homework 10 1. Write the correct first-line syntax for a user defined function that is named Feynman and has input variables q and e, and outputs L and M. (Comment out of this line). 2. Using zeros, ones, and eye commands write the one line syntax to create the following matrix: 00000 01100 01010 01001 3. Write the syntax for the user defined function that calculates the surface area and volume of a...

  • I need help creating a Mean Filter for Matlab 3 by 3. ( I need the...

    I need help creating a Mean Filter for Matlab 3 by 3. ( I need the Actual Matlab WORking code) It's dude tomorrow, final project worth 40% of my grade. It can not use Matlab Function, must be user define fuction. So far what I have is, I am not sure how to start this, I was told it requires 2 for loops A= imread('lena_256.tif'); % Reads the image in matrix M = ones(3,3)/9; % Creates a matrix of 3...

  • I need some help with this Python Program. I'm having trouble with getting a output 1...

    I need some help with this Python Program. I'm having trouble with getting a output 1 of the 5 required. Tried twice and it wont pop up. Output#1 Nguyen, John Bonus is $0 × |ls Python 3,7 3 Shell S classwork/Lab 8 py 【3.73) Python 3.7.3 (T3.7. 3: efte。6ed12, Baz 25 2019. 211261 53) [ASC v.1916 32 bit IIntel; ve "help pripht" xedis" "licenseox oze infomation thiaYearnitine input (Enter chis geax s uni HISYEARSUNIT3-1001 ธ.pr print'Bonus i8.fomat (BONU4

  • I'm having trouble with numbers 5 to 7. I need help working out the equation in...

    I'm having trouble with numbers 5 to 7. I need help working out the equation in question 5 so I can graph it for number 6 and using the slope from the graph to solve number 7. 1. 2 points Measure ands record the em.f E of the given cell. E = 5.65 v 2. Connect an external resistance R=12 and measure the terminal voltage V across the resistor. 2 points 1 ohm = 4.42 3. 1 Repeat step 2...

  • I'm having trouble with numbers 5 to 7. I need help working out the equation in...

    I'm having trouble with numbers 5 to 7. I need help working out the equation in question 5 so I can graph it for number 6 and using the slope from the graph to solve number 7. 1. 2 points Measure ands record the em.f E of the given cell. E = 5.65 v 2. Connect an external resistance R=12 and measure the terminal voltage V across the resistor. 2 points 1 ohm = 4.42 3. 1 Repeat step 2...

  • Hello I need help with the following I'm having trouble getting to just the main code....

    Hello I need help with the following I'm having trouble getting to just the main code. Attached is the ButtonDebounce header file and the description of the code the language is C++; This lab will end with a functioning combination lock. The lock will have a combination that resides in EEPROM, and is programmable via the serial port. The user will enter a code, using the button and encoder on the board, and is shown on the lcd screen. Once...

  • Using Matlab or Octave, I do not know how to write the script for this problem,...

    Using Matlab or Octave, I do not know how to write the script for this problem, please help. Must match sample output 5. Consider a 3 x 3 matrix given by A = 01 012 013 az az 633 ( dai 632 033 The trace of A is defined as tr(A)=2au. Given another 3 x 3 matrix bu bı2 bia B- bi brz bra | bgi bz2 by it can be shown that trAB') = tr(AB). Write a script that...

  • I am having a hard time writing an IDLE code (Python 3.7.2) and I need help....

    I am having a hard time writing an IDLE code (Python 3.7.2) and I need help. Here are the parameters. 1. Write a program that will generate a random backpack. This program will have a menu that allows the user to continuously select between generating a new backpack, printing the current backpack, adding a given number of an item, removing a given number of an item or quitting. Every backpack will have a variable number of the following 7 items:...

  • I'm trying to write this program in C++ and I'm having trouble initializing the overload function...

    I'm trying to write this program in C++ and I'm having trouble initializing the overload function I have to use. Here's the assignment and what I got so far, thanks for the help! /*2. Implement the game of Rock-Paper-Scissors in C++, where you can play against the computer. a. Scissors beat paper, paper beats rock, and rock beats scissors. If both you and computer choose the same tool, it is a tie. b. Your program should have a class game...

  • I need help with coding with c++. I need to make a lottery program where you...

    I need help with coding with c++. I need to make a lottery program where you enter in 5 numbers from 0-9 and the program will generate 5 random numbers 0-9 to represent the lottery numbers. I'm almost done, but the output isn't exactly what I want. It works fine, but the format should look like this: lottery array: 7 4 9 1 3 user array: 4 2 9 7 p.s. I also need to count how many matching numbers...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT