From bbb7e5647f6fb138aa11cc2933160b10f48a6581 Mon Sep 17 00:00:00 2001
From: Garifullin-Farid <95081032+Garifullin-Farid@users.noreply.github.com>
Date: Mon, 20 May 2024 19:39:00 +0400
Subject: [PATCH] 11
---
.idea/.gitignore | 3 +
Lab_3/.gitignore | 29 ++++++++
Lab_3/.idea/.gitignore | 3 +
Lab_3/.idea/misc.xml | 6 ++
Lab_3/.idea/modules.xml | 8 +++
Lab_3/.idea/uiDesigner.xml | 124 ++++++++++++++++++++++++++++++++
Lab_3/Lab_3.iml | 11 +++
Lab_3/src/Main.java | 49 +++++++++++++
Lab_3/src/task1/Heap.java | 129 ++++++++++++++++++++++++++++++++++
Lab_3/src/task1/Node.java | 17 +++++
Lab_3/src/task2/Graph.java | 64 +++++++++++++++++
Lab_3/src/task2/Node.java | 21 ++++++
lab-2/.gitignore | 29 ++++++++
lab-2/.idea/.gitignore | 3 +
lab-2/.idea/misc.xml | 6 ++
lab-2/.idea/modules.xml | 8 +++
lab-2/lab-2.iml | 11 +++
lab-2/src/Main.java | 20 ++++++
lab-2/src/MainBruteforce.java | 57 +++++++++++++++
lab-2/src/Task_2.java | 58 +++++++++++++++
lab-2/src/task_1.java | 61 ++++++++++++++++
21 files changed, 717 insertions(+)
create mode 100644 .idea/.gitignore
create mode 100644 Lab_3/.gitignore
create mode 100644 Lab_3/.idea/.gitignore
create mode 100644 Lab_3/.idea/misc.xml
create mode 100644 Lab_3/.idea/modules.xml
create mode 100644 Lab_3/.idea/uiDesigner.xml
create mode 100644 Lab_3/Lab_3.iml
create mode 100644 Lab_3/src/Main.java
create mode 100644 Lab_3/src/task1/Heap.java
create mode 100644 Lab_3/src/task1/Node.java
create mode 100644 Lab_3/src/task2/Graph.java
create mode 100644 Lab_3/src/task2/Node.java
create mode 100644 lab-2/.gitignore
create mode 100644 lab-2/.idea/.gitignore
create mode 100644 lab-2/.idea/misc.xml
create mode 100644 lab-2/.idea/modules.xml
create mode 100644 lab-2/lab-2.iml
create mode 100644 lab-2/src/Main.java
create mode 100644 lab-2/src/MainBruteforce.java
create mode 100644 lab-2/src/Task_2.java
create mode 100644 lab-2/src/task_1.java
diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..26d3352
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,3 @@
+# Default ignored files
+/shelf/
+/workspace.xml
diff --git a/Lab_3/.gitignore b/Lab_3/.gitignore
new file mode 100644
index 0000000..f68d109
--- /dev/null
+++ b/Lab_3/.gitignore
@@ -0,0 +1,29 @@
+### IntelliJ IDEA ###
+out/
+!**/src/main/**/out/
+!**/src/test/**/out/
+
+### Eclipse ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+bin/
+!**/src/main/**/bin/
+!**/src/test/**/bin/
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+
+### VS Code ###
+.vscode/
+
+### Mac OS ###
+.DS_Store
\ No newline at end of file
diff --git a/Lab_3/.idea/.gitignore b/Lab_3/.idea/.gitignore
new file mode 100644
index 0000000..26d3352
--- /dev/null
+++ b/Lab_3/.idea/.gitignore
@@ -0,0 +1,3 @@
+# Default ignored files
+/shelf/
+/workspace.xml
diff --git a/Lab_3/.idea/misc.xml b/Lab_3/.idea/misc.xml
new file mode 100644
index 0000000..45b76e1
--- /dev/null
+++ b/Lab_3/.idea/misc.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Lab_3/.idea/modules.xml b/Lab_3/.idea/modules.xml
new file mode 100644
index 0000000..9634eea
--- /dev/null
+++ b/Lab_3/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Lab_3/.idea/uiDesigner.xml b/Lab_3/.idea/uiDesigner.xml
new file mode 100644
index 0000000..2b63946
--- /dev/null
+++ b/Lab_3/.idea/uiDesigner.xml
@@ -0,0 +1,124 @@
+
+
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+
+
+ -
+
+
+ -
+
+
+
+
+
\ No newline at end of file
diff --git a/Lab_3/Lab_3.iml b/Lab_3/Lab_3.iml
new file mode 100644
index 0000000..c90834f
--- /dev/null
+++ b/Lab_3/Lab_3.iml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Lab_3/src/Main.java b/Lab_3/src/Main.java
new file mode 100644
index 0000000..e6fffd1
--- /dev/null
+++ b/Lab_3/src/Main.java
@@ -0,0 +1,49 @@
+import task1.Heap;
+import task2.Graph;
+
+public class Main {
+ public static void main(String[] args) {
+ // задаем начальные данные:
+ Heap heap = new Heap(31);
+ heap.insertNode(120);
+ heap.insertNode(40);
+ heap.insertNode(50);
+ heap.insertNode(80);
+ heap.insertNode(20);
+ heap.insertNode(100);
+ heap.insertNode(150);
+ heap.insertNode(30);
+ heap.insertNode(210);
+ heap.insertNode(180);
+ heap.insertNode(10);
+ heap.insertNode(90);
+ // выводим начальную пирамиду в консоль
+ heap.printHeap();
+ heap.changeNode(0,15);
+ heap.printHeap();
+ heap.removeNode(3);
+ heap.printHeap();
+
+
+
+// Graph g = new Graph(5);
+//
+// g.addEdge(0, 1);
+// g.addEdge(0, 2);
+// g.addEdge(1, 2);
+// g.addEdge(2, 0);
+// g.addEdge(2, 3);
+// g.addEdge(3, 3);
+//
+// g.addEdge(0,1);
+// g.addEdge(0,4);
+// g.addEdge(1, 2);
+// g.addEdge(1, 3);
+// g.addEdge(1, 4);
+// g.addEdge(2, 3);
+/// g.addEdge(3, 4);
+//
+//
+// g.BFS(1);
+ }
+}
\ No newline at end of file
diff --git a/Lab_3/src/task1/Heap.java b/Lab_3/src/task1/Heap.java
new file mode 100644
index 0000000..f4732bd
--- /dev/null
+++ b/Lab_3/src/task1/Heap.java
@@ -0,0 +1,129 @@
+package task1;
+
+import task1.Node;
+
+public class Heap {
+ private Node[] heapArray; // массив со всеми вершинами
+ private int maxSize; // размер массива
+ private int currentSize; // количество узлов массиве
+
+ public Heap(int maxSize) {
+ this.maxSize = maxSize;
+ this.currentSize = 0;
+ heapArray = new Node[maxSize];
+ }
+
+ public void printHeap() {
+ System.out.println("Массив значений: ");
+
+ for (int n = 0; n < currentSize; n++) {
+ if (heapArray[n] != null) {
+ System.out.println(heapArray[n].getValue() + " ");
+ }
+ else {
+ System.out.println("-");
+ }
+ }
+ System.out.println();
+
+ int countOfGaps = 32;
+ int itemsPerRow = 1;
+ int columnNumber = 0; // номер элемента в данной строке
+ String lines = "___________________________________________________________________";
+ System.out.println(lines);
+ for (int i = 0; i < currentSize; i++) {
+ if (columnNumber == 0) { // проверяем первый элемент ли в текущей строке
+ for (int k = 0; k < countOfGaps; k++) { // добавляем предшествующие пробелы
+ System.out.print(' ');
+ }
+ }
+ System.out.print(heapArray[i].getValue());// выводим в консоль значение вершины
+
+ if (++columnNumber == itemsPerRow) {
+ countOfGaps /= 2;
+ itemsPerRow *= 2;
+ columnNumber = 0;
+ System.out.println(); // переходим на нову строку
+ }
+ else {
+ for (int k = 0; k < countOfGaps * 2 - 2; k++) {
+ System.out.print(' '); // добавляем оступы
+ }
+ }
+ }
+ System.out.println("\n" + lines); // нижний пункир
+ }
+
+ public boolean insertNode(int value) { // вставка нового значения
+ if (currentSize >= maxSize) {
+ return false;
+ }
+ Node newNode = new Node(value);
+ heapArray[currentSize] = newNode;
+ displaceUp(currentSize++);
+ return true;
+ }
+
+ public Node removeNode(int index) { // удалить элемент по индексу массива
+ if(index > 0 && currentSize > index) {
+ Node root = heapArray[index];
+ heapArray[index] = heapArray[--currentSize]; // задаём элементу с переданным индексом, значение последнего элемента
+ heapArray[currentSize] = null;// последний элемент удаляем
+ displaceDown(index);// проталкиваем вниз новый элемент, чтобы он должное ему место
+ return root;
+ }
+ return null;
+ }
+
+ public boolean changeNode(int index, int newValue) {
+ if (index < 0 || currentSize<=index) {
+ return false;
+ }
+ int oldValue = heapArray[index].getValue(); // сохраняем старое значение
+ heapArray[index].setValue(newValue); // присваиваем новое
+
+ if (oldValue < newValue) {// если узел повышается
+ displaceUp(index); // выполняется смещение вверх
+ }
+ else { // если понижается
+ displaceDown(index); // смещение вниз
+ }
+ return true;
+ }
+
+ private void displaceUp(int index) { //смещение вверх
+ int parentIndex = (index - 1) / 2; // узнаем индекс родителя
+ Node bottom = heapArray[index]; // берем элемент
+ while (index > 0 && heapArray[parentIndex].getValue() < bottom.getValue()) {// если родительский элемент меньше
+ heapArray[index] = heapArray[parentIndex];// то меняем его местами с рассматриваемым
+ index = parentIndex;
+ parentIndex = (parentIndex - 1) / 2;// берем новый родительский индекс и повторяем сравнение элементов
+ }
+ heapArray[index] = bottom;// соохраняем результат
+ }
+
+ private void displaceDown(int index) {// смещение вниз
+ int largerChild;
+ Node top = heapArray[index]; // сохранение корня, пока у узла есть хотя бы один потомок
+ while (index < currentSize / 2) {// если данное условие не выполняется то элемент уже в самом низу пирамиды
+ int leftChild = 2 * index + 1; // вычисляем индексы в массиве для левого узла ребенка
+ int rightChild = leftChild + 1;// и правого
+
+ if (rightChild < currentSize && heapArray[leftChild].getValue() < heapArray[rightChild].getValue()) {
+ largerChild = rightChild;
+ }// вычисляем ребенка вершину с наибольшим числовым значением
+ else {
+ largerChild = leftChild;
+ }
+
+ if (top.getValue() >= heapArray[largerChild].getValue()) {// если значение вершины больше или равно
+ //значени его наибольшего ребенка
+ break;// то выходим из метода
+ }
+
+ heapArray[index] = heapArray[largerChild];// заменяем вершину, большей дочерней вершиной
+ index = largerChild; // текущий индекс переходит вниз
+ }
+ heapArray[index] = top; // задаем конечное местоположение для элемента
+ }
+}
\ No newline at end of file
diff --git a/Lab_3/src/task1/Node.java b/Lab_3/src/task1/Node.java
new file mode 100644
index 0000000..57017ee
--- /dev/null
+++ b/Lab_3/src/task1/Node.java
@@ -0,0 +1,17 @@
+package task1;
+
+public class Node {
+ private int value;
+
+ public Node(int value) {
+ this.value = value;
+ }
+
+ public int getValue() {
+ return this.value;
+ }
+
+ public void setValue(int value) {
+ this.value = value;
+ }
+}
\ No newline at end of file
diff --git a/Lab_3/src/task2/Graph.java b/Lab_3/src/task2/Graph.java
new file mode 100644
index 0000000..7ba8b01
--- /dev/null
+++ b/Lab_3/src/task2/Graph.java
@@ -0,0 +1,64 @@
+package task2;
+
+
+// Java program to print BFS traversal from a given source vertex.
+// BFS(int s) traverses vertices reachable from s.
+import java.io.*;
+import java.util.*;
+
+// This class represents a directed graph using adjacency list
+// representation
+public class Graph
+{
+ private int V; // No. of vertices
+ private LinkedList adj[]; //Adjacency Lists
+
+ // Constructor
+ public Graph(int v)
+ {
+ V = v;
+ adj = new LinkedList[v];
+ for (int i=0; i queue = new LinkedList();
+
+
+ visited[s]=true;
+ queue.add(s);
+
+ while (queue.size() != 0)
+ {
+ s = queue.poll();
+ System.out.print(s+" ");
+
+
+ Iterator i = adj[s].listIterator();
+ while (i.hasNext())
+ {
+ int n = i.next();
+ if (!visited[n])
+ {
+ visited[n] = true;
+ queue.add(n);
+ }
+ }
+ }
+ }
+
+
+}
diff --git a/Lab_3/src/task2/Node.java b/Lab_3/src/task2/Node.java
new file mode 100644
index 0000000..161054b
--- /dev/null
+++ b/Lab_3/src/task2/Node.java
@@ -0,0 +1,21 @@
+package task2;
+
+public class Node {
+ int n;
+ String name;
+ boolean visited;
+
+ Node(int n, String name) {
+ this.n = n;
+ this.name = name;
+ visited = false;
+ }
+
+ void visit() {
+ visited = true;
+ }
+
+ void unvisit() {
+ visited = false;
+ }
+}
\ No newline at end of file
diff --git a/lab-2/.gitignore b/lab-2/.gitignore
new file mode 100644
index 0000000..f68d109
--- /dev/null
+++ b/lab-2/.gitignore
@@ -0,0 +1,29 @@
+### IntelliJ IDEA ###
+out/
+!**/src/main/**/out/
+!**/src/test/**/out/
+
+### Eclipse ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+bin/
+!**/src/main/**/bin/
+!**/src/test/**/bin/
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+
+### VS Code ###
+.vscode/
+
+### Mac OS ###
+.DS_Store
\ No newline at end of file
diff --git a/lab-2/.idea/.gitignore b/lab-2/.idea/.gitignore
new file mode 100644
index 0000000..26d3352
--- /dev/null
+++ b/lab-2/.idea/.gitignore
@@ -0,0 +1,3 @@
+# Default ignored files
+/shelf/
+/workspace.xml
diff --git a/lab-2/.idea/misc.xml b/lab-2/.idea/misc.xml
new file mode 100644
index 0000000..779255b
--- /dev/null
+++ b/lab-2/.idea/misc.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/lab-2/.idea/modules.xml b/lab-2/.idea/modules.xml
new file mode 100644
index 0000000..186eae5
--- /dev/null
+++ b/lab-2/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/lab-2/lab-2.iml b/lab-2/lab-2.iml
new file mode 100644
index 0000000..c90834f
--- /dev/null
+++ b/lab-2/lab-2.iml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/lab-2/src/Main.java b/lab-2/src/Main.java
new file mode 100644
index 0000000..69ae178
--- /dev/null
+++ b/lab-2/src/Main.java
@@ -0,0 +1,20 @@
+public class Main {
+
+
+
+
+
+ public static void main(String[] args) {
+ task_1 t1 = new task_1();
+ t1.aa();
+ t1.Sorter();
+ t1.printer(t1.b);
+ t1.printer(t1.wp);
+ t1.printer(t1.v);
+ t1.S();
+
+ Task_2 t2 = new Task_2();
+ t2.Task_2();
+
+ }
+}
\ No newline at end of file
diff --git a/lab-2/src/MainBruteforce.java b/lab-2/src/MainBruteforce.java
new file mode 100644
index 0000000..70124c6
--- /dev/null
+++ b/lab-2/src/MainBruteforce.java
@@ -0,0 +1,57 @@
+public class MainBruteforce {
+ public static void main(String[] args) {
+ int[] weights = {3, 4, 5, 8, 9};
+ int[] prices = {1, 6, 4, 7, 6};
+
+ int maxWeight = 13;
+
+ long count = (long) Math.pow(2, maxWeight);
+
+ int maxPrice = 0;
+ long maxState = 0;
+
+ for (long state = 0; state < count; state++) {
+ int price = statePrice(state, prices);
+ int weight = stateWeight(state, weights);
+ if (weight <= maxWeight) {
+ if (maxPrice < price) {
+ maxPrice = price;
+ maxState = state;
+ }
+ }
+ }
+
+ System.out.println("Оптимальное содержимое рюкзака:");
+ long poverOfTwo = 1;
+ for (int i = 0; i < weights.length; i++) {
+ if ((poverOfTwo & maxState) > 0) {
+ System.out.println(i + 1);
+ }
+ poverOfTwo <<= 1;
+ }
+ }
+
+ private static int stateWeight(long state, int[] weights) {
+ long poverOfTwo = 1;
+ int weight = 0;
+ for (int i = 0; i < weights.length; i++) {
+ if ((poverOfTwo & state) != 0) {
+ weight += weights[i];
+ }
+ poverOfTwo <<= 1;
+ }
+ return weight;
+ }
+
+ private static int statePrice(long state, int[] prices) {
+ long poverOfTwo = 1;
+ int price = 0;
+ for (int i = 0; i < prices.length; i++) {
+ if ((poverOfTwo & state) != 0) {
+ price += prices[i];
+ }
+ poverOfTwo <<= 1;
+ }
+ return price;
+ }
+}
\ No newline at end of file
diff --git a/lab-2/src/Task_2.java b/lab-2/src/Task_2.java
new file mode 100644
index 0000000..2f32025
--- /dev/null
+++ b/lab-2/src/Task_2.java
@@ -0,0 +1,58 @@
+import java.util.ArrayList;
+
+public class Task_2 {
+
+ public void Task_2() {
+ int[] weights = {3, 4, 5, 8, 9};
+ int[] prices = {1, 6, 4, 7, 6};
+
+ int count = weights.length;
+ int maxWeight = 13;
+ int[][] A;
+ A = new int[count + 1][];
+ for (int i = 0; i < count + 1; i++) {
+ A[i] = new int[maxWeight + 1];
+ }
+ //К - количество предметов
+ //S - текущий вес рюкзака
+ // A[k][s] - максимальная стоймость для соответсвующего рюкзака
+
+ for (int k = 0; k <= count; k++) {
+ for (int s = 0; s <= maxWeight; s++) {
+ if (k == 0 || s == 0) {
+ A[k][s] = 0;
+ } else {
+ if (s >= weights[k - 1]) {
+ A[k][s] = Math.max(A[k - 1][s], A[k - 1][s - weights[k - 1]] + prices[k - 1]);
+ } else {
+ A[k][s] = A[k - 1][s];
+ }
+ }
+ }
+
+ }
+
+// ArrayList result = new ArrayList<>();
+// traceResult(A, weights, count, maxWeight, result);
+//
+// System.out.println("Оптимальное содержимое рюкзака:");
+// for(Integer integer : result) {
+// System.out.println(integer);
+// }
+// }
+//
+// private static void traceResult(int[][] A, int[] weight, int k, int s, ArrayList result) {
+// if (A[k][s] == 0) {
+// return;
+// }
+// if (A[k -1][s] == A[k][s]) {
+// traceResult(A, weight, k - 1, s, result);
+// } else {
+// traceResult(A, weight, k - 1, s - weight[k - 1], result);
+// result.add(0, k);
+// }
+
+ }
+
+}
+
diff --git a/lab-2/src/task_1.java b/lab-2/src/task_1.java
new file mode 100644
index 0000000..af42d71
--- /dev/null
+++ b/lab-2/src/task_1.java
@@ -0,0 +1,61 @@
+import java.util.Arrays;
+
+public class task_1 {
+ int n = 3;
+ int w =40;
+ int[] wp ={10,20,20};
+ int[] v ={60,100,100};
+ int [] b={0,0,0};
+
+ int s = 0;
+
+ private void toSwap(int first, int second){
+ int d = wp[first];
+ wp[first] = wp[second];
+ wp[second] = d;
+
+ int d1 =b[first];
+ b[first] = b[second];
+ b[second] = d1;
+
+ int d2 =v[first];
+ v[first] = v[second];
+ v[second] = d2;
+ }
+
+ public void Sorter(){
+ for (int out = n - 1; out >= 1; out--){
+ for (int in = 0; in < out; in++){
+ if(b[in] < b[in + 1])
+ toSwap(in, in + 1);
+ }
+ }
+ }
+
+ public void aa(){
+ for(int i = 0; i < n; i++) {
+ int a = v[i]/wp[i];
+ b[i] = a;
+ }
+ }
+ public void S(){
+ int l = 40;
+ for (int i = 0; i0){
+ s+=v[i];
+ l-=wp[i];
+ }
+ }
+ System.out.println(s);
+ }
+
+
+ public void printer(int[] a){
+ for (int i = 0; i < n; i++){
+ System.out.print(a[i] + " ");
+
+ }
+ System.out.println(" ");
+ System.out.println("---------------");
+ }
+}