0% found this document useful (0 votes)
9 views26 pages

Daa Lab Eval 1

The document contains code implementations for graph algorithms, including depth-first search (DFS) for finding the diameter of a tree and Kruskal's algorithm for constructing a maximum spanning tree. It includes Python and C++ code snippets demonstrating these algorithms with example graph structures. The document also discusses input handling and output results for the implemented algorithms.

Uploaded by

gsailesh02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views26 pages

Daa Lab Eval 1

The document contains code implementations for graph algorithms, including depth-first search (DFS) for finding the diameter of a tree and Kruskal's algorithm for constructing a maximum spanning tree. It includes Python and C++ code snippets demonstrating these algorithms with example graph structures. The document also discusses input handling and output results for the implemented algorithms.

Uploaded by

gsailesh02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

DAA LAB EVALUATION-1

DAA LAB EVALUATION-1


DAA LAB EVALUATION-1
DAA LAB EVALUATION-1

class graph:

def findmax4(self, arr, i, arr1=[], x=0):

if x == 0:

print("For better image quality from the ", i ,"station are", end=' ')

if x < 5:

if i not in arr1:

print(i, end=' ')

arr1.append(i)

x += 1

if x < 5:

for j in arr[i]:

self.findmax4(arr, j, arr1, x)

if x == 1:

print("\n")

def traverse(self, arr, s):

global vis

kk = []

vis[s] = True

kk.append(s)

self.findmax4(arr, s, [])

while (kk):
DAA LAB EVALUATION-1

a = kk.pop(0)

for i in arr[a]:

if vis[i] == False:

self.findmax4(arr, i, [])

kk.append(i)

vis[i] = True

arr = []

for i in range(10):

arr.append([])

gr = graph()

arr = [[1,2,3],[0],[0],[0,4],[3,5,6],[4,8],[4,7],[6],[5,9],[8]]

vis = []

for i in range(10):

vis.append(False)

gr.traverse(arr, 0)
DAA LAB EVALUATION-1

2.
DAA LAB EVALUATION-1

#include <iostream>

#include <vector>

using namespace std;

int Diameter = -1, maxNode = -1;

int vis[8];

void createEdge(int a, int b, vector<int> graph[])

graph[a].push_back(b);

graph[b].push_back(a);

void DFS(vector<int> graph[], int node, int d)

vis[node] = 1;

if (d > Diameter)

maxNode = node;

Diameter = d;

for (auto x : graph[node])

if (vis[x] == 0)

DFS(graph, x, d + 1);

}
DAA LAB EVALUATION-1

int main()

vector<int> graph[8];

createEdge(4, 5, graph);

createEdge(2, 3, graph);

createEdge(2, 9, graph);

createEdge(2, 4, graph);

createEdge(1, 7, graph);

createEdge(4, 6, graph);

DFS(graph, 1, 1);

maxD = -1;

for (int i = 1; i <= 8; i++)

vis[i] = 0;

DFS(graph, maxNode, 1);

cout << Diameter << " is the diameter of the tree " << endl;

return 0;

3.
DAA LAB EVALUATION-1

import os
DAA LAB EVALUATION-1

import sys

def main():

#Reading file name

inFile =input("\nEnter input file name: ")

if not os.path.isfile(inFile):

#if file not found then exits

print '\nFile not found.\nExiting...\n'

sys.exit(1)

inp = open(inFile, 'r')

table = {}

tree = []

weight = 0

for line in inp.readlines():

temp = line.split(' ')

weight = 0 - int(temp[2])

if weight != 0:

table[tuple(temp[:2])] = weight

weight = kruskal(table, tree)

print "\nThe maximum spanning tree: \n"

print tree

print "\nThe maximum benefit is: %d\n" % weight

def valid(key, tree):


DAA LAB EVALUATION-1

temp1 = []

temp2 = []

for k in tree:

if key[0] in list(k):

for i in list(k):

if i != key[0]:

temp1.append(i)

if key[1] in list(k):

for i in list(k):

if i != key[1]:

temp2.append(i)

for i in temp1:

for j in temp2:

if i == j:

return False

return True

def kruskal(table, tree):

i=0

weight = 0

v = int(raw_input("\nNo. of vertices: "))

for key, value in sorted(table.items()):

print value

if valid(key, tree):

tree.append(key)
DAA LAB EVALUATION-1

weight += (0 - value)

i += 1

if i == (v - 1):

break

return weight

4.
DAA LAB EVALUATION-1
DAA LAB EVALUATION-1
DAA LAB EVALUATION-1
DAA LAB EVALUATION-1
DAA LAB EVALUATION-1
DAA LAB EVALUATION-1
DAA LAB EVALUATION-1
DAA LAB EVALUATION-1
DAA LAB EVALUATION-1
DAA LAB EVALUATION-1
DAA LAB EVALUATION-1

IMPLEMENTATION WITH OUTPUT:


DAA LAB EVALUATION-1
DAA LAB EVALUATION-1
DAA LAB EVALUATION-1

You might also like