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...

  • 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...

  • HI, I am having trouble finsihing this program i was wondering if someone could help me....

    HI, I am having trouble finsihing this program i was wondering if someone could help me. Thanks in adavnce. Here is the code that I have so far..... //Stack.h // implementation file for the stack class #include <iostream> using namespace std; const int stack_size = 100; class stack { private: char data [stack_size]; // elements in the stack int top; // index of the top element of the stack public: stack (); // constructor creates an empty stack void push...

  • I need help solving these questions,please don’t know how to solve it by using matlab.!

    i need help solving these questions,please don’t know how to solve it by using matlab.! Solve the following questions Ol: You are required to develop a script to calculate the volume of a pyramid, which is 1/3 *base *height, where the base is length *width. Prompt the user to enter values for the length, width, and height, and then calculate the volume of the pyramid. When the user enters each value, he or she will then also be prompted to...

  • i'm having trouble with 1 and 3 using html, javascript and jquery 1) Change the "Search"...

    i'm having trouble with 1 and 3 using html, javascript and jquery 1) Change the "Search" link on the left to a button that when clicked, hide all the fields on the form and display the text "This is a search feature..." 3) When the user fills the form and clicks "Login", the values inputted should be displayed in the empty row (under Login) of the page. The display should be in the form: Student ID: <input value> Student Name:...

  • I'm having trouble working with a .csv file in C Programming. I know that I need...

    I'm having trouble working with a .csv file in C Programming. I know that I need to use parallel arrays to search through the data but I am a little lost. How would I go through the data in a file, for example I created one and attached it. To find the information between two dates. So Ask the user for two dates then search the .csv file for that and give an output similar to: "Select the starting and...

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
ADVERTISEMENT