Sunteți pe pagina 1din 10

Write a program in C to find indegree and outdegree of every vertex of a directed graph when graph is represented by an adjacency list

. NameRoll no- 063 #include<stdio.h> void main() { int m[10][10],r,c,sumin,sumout,n,v,i; clrscr(); printf("how many vertices:"); scanf("%d",&n); for(r=0;r<n;r++) for(c=0;c<n;c++) { m[r][c]=0; if(r!=c) { printf("is there an edge between vertex %d and %d (1/0):",r+1,c+1); scanf("%d",&m[r][c]); } } printf("\n\nvertex\tindegree\tOutdegree\ttotal degree\n"); for(v=0;v<n;v++) { sumin=sumout=0; for(i=0;i<n;i++) { sumin=sumout+m[i][v]; sumout=sumout+m[v][i]; } printf("%d\t\t%d\t\t%d\t\t%d\n",v+1,sumin,sumout,sumin+sumout); } getch(); } /* output how many vertices:2 is there an edge between vertex 1 and 2 (1/0):1

is there an edge between vertex 2 and 1 (1/0):1

vertex indegree 1 2 1 1 1 1

Outdegree 2

total degree

2 */

Write a program in C to implement DFS on a graph through an adjacency matrix. Name-Shraddha Wani Roll no- -63 #include<stdio.h> #define MAX 20 typedef struct { int data[MAX]; int top; } STACK; void initstack(STACK *ps) { ps->top=-1;} void push(STACK *ps, int num) { ps->top++; ps->data[ps->top]=num; } int pop(STACK *ps) { return(ps->data[ps->top--]); } int isempty(STACK *ps) { return(ps->top== -1); } int isfull(STACK *ps) { return( ps->top==MAX-1); } void dfs(int m[10][10],int n) { int i,v,w,found; int visited[20]={0}; STACK s; initstack(&s); v=0;

visited[v]=1; push(&s,v); printf("v%d",v+1); visited[w]=1; while(1) { found=0; for(w=0;w<n;w++) { if((m[v][w]==1)&&(visited[w]==0)) { push(&s,w); printf("v%d",w+1); visited[w]=1; v=w; found=1; break; } } if(found==0) if(isempty(&s)) break; else v=pop(&s); } } void main() { int m[10][10],n,i,j; clrscr(); printf("\nhow many vertices:"); scanf("%d",&n); for(i=0;i<n;i++) for(j=0;j<n;j++) { if(i!=j) { printf("is there an edge between vertex %d and %d (1/0)",i+1,j+1);

scanf("%d", &m[i][j]); } }

printf("\nthe depth first search is :"); dfs(m,n); getch(); } /* output how many vertices:2 is there an edge between vertex 1 and 2 (1/0)1 is there an edge between vertex 2 and 1 (1/0)1 the depth first search is :v1v2

Name- Shraddha Wani Roll no- 063 Consider the following Entities & Relationships Politician (pno, pname, pdesc, constituency) Party (party_code,party_name) Politician & party are related with many-to-one. Constraints : Primary key, Foreign key Party_name Not NULL Create a RDB in 3NF & construct the queries in SQL Server/Oracle 8i 1.List all politicians of party BJP. 2. Count the number of politician having political description as MP and constituency is North Delhi. 3. Count the total number of politicians for each party. 4. List party wise politician names along with their constituency
SQL> create table party32 2 (party_code integer primary key, 3 party_name varchar2(20) not null);

Table created.

SQL> create table politician32 2 (pcode integer primary key, 3 pname varchar2(10),pdesc varchar2(20), 4 constituency varchar2(20),party_code 5 integer,foreign key(party_code) 6 references party32);

Table created. SQL> select * from party32;

PARTY_NO PNAME --------- -------------------101 BJP 102 CNG 103 MNS

SQL> select * from politician32;

PCODE PNAME

PDESC

CONSTITUENCY

PARTY_CODE

--------- -------------------- -------------------- -------------------- ---------1 kamble 2 sinha 3 ahuja 4 shelar 5 yadav 6 singh 7 rao MLA MP MP MLA MLA nasik MP north mumbai north mumbai north delhi pune north delhi nasik west mumbai 103 102 103 101 102 103 101

1) List all politicians of party BJP.

SQL> select party32.pname,politician32.pname 2 from party32,politician32

3 where party32.party_no=politician32.party_code 4 and party32.pname='BJP' 5 group by party32.pname,politician32.pname;

PNAME

PNAME

-------------------- ---------BJP BJP sinha yadav

2) Count the number of politician having political description as MP and

constituency is

North Delhi.

SQL> select politician32.pname,pdesc,constituency 2 from party32,politician32 3 where party32.party_no= politician32.party_code 4 and pdesc='MP' and constituency='north delhi' 5 group by politician32.pname,pdesc,constituency;

PNAME

PDESC

CONSTITUENCY

-------------------- -------------------- -------------------ahuja MP north delhi

3) Count the total number of politicians for each party SQL> select party32.pname,count(pcode) 2 from party32,politician32 3 where party32.party_no=politician32.party_code

4 group by party32.pname;

PNAME

COUNT(PCODE)

-------------------- -----------BJP CNG MNS 2 2 3

4) List party wise politician names along with their constituency

SQL> select party32.pname,politician32.pname,constituency 2 from party32,politician32 3 where party32.party_no=politician32.party_code 4 group by party32.pname,politician32.pname,constituency;

PNAME

PNAME

CONSTITUENCY

-------------------- ---------- -------------------BJP BJP CNG CNG MNS MNS MNS sinha yadav ahuja rao kamble shelar singh north delhi north delhi north delhi west mumbai north mumbai pune nasik

7 rows selected.

S-ar putea să vă placă și