-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprims.html
More file actions
194 lines (179 loc) · 7.44 KB
/
prims.html
File metadata and controls
194 lines (179 loc) · 7.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Greedy Techniques</title>
<link href="https://fonts.googleapis.com/css2?family=Baloo+Tamma+2:wght@500;600&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"
integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"
integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"
integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6"
crossorigin="anonymous"></script>
<link rel="stylesheet" href="style.css">
<script src="prims.js"></script>
<link rel='icon' href='https://upload.wikimedia.org/wikipedia/commons/3/39/Identity_graph2.svg'>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
<a class="navbar-brand" href="index.html">Greedy Techniques</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarTogglerDemo02">
<ul class="navbar-nav ml-auto mt-2 mt-lg-0">
<li class="nav-item ">
<a class="nav-link" href="huffman.html">Huffman Coding</a>
</li>
<li class="nav-item active" >
<a class="nav-link" href="prims.html" >Prim's algorithm</a>
</li>
<li class="nav-item" >
<a class="nav-link" href="knapsack.html" >Knapsack Problem</a>
</li>
<li class="nav-item" >
<a class="nav-link" href="task.html">Task Scheduling</a>
</li>
<li class="nav-item " >
<a class="nav-link" href="djikstra.html">Single Source Shortest Path</a>
</li>
</ul>
</div>
</nav>
<br>
<br>
<section class="container mt-5">
<div class="jumbotron ">
<h1 >Prim's Algorithm</h1>
<p class="lead">About the algorithm:</p><hr>
<p>Prim's algorithm is a minimum spanning tree algorithm that takes a graph as input and finds the subset of the edges of that graph which </p>
<ul>
<li> forms a tree that includes every vertex</li>
<li>has the minimum sum of weights among all the trees that can be formed from the graph</li>
</ul>
<hr>
<h3>Time Complexity:O(ElogV)</h3>
</div>
</section>
<section>
<div class="container mb-5">
<div class="row">
<div class="col-lg-4">
<div class="card bg-light">
<h5 class="card-header">Input:</h5>
<div class="card-body">
<div class="input-group">
<p>Enter the number of vertices in your graph:</p>
<input type="number" id="vertices" class="form-control">
<div class="input-group-append" id="button-addon4">
<button class="btn btn-info" id="button" type="button">Submit</button>
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-8 " id="table" style="visibility: hidden;">
<div class="card bg-light">
<h5 class="card-header">Output:</h5>
<div class="card-body">
<div class="container table-responsive fixed-table-body" >
<table class="table">
<thead>
<tr id="table_headers">
</tr>
</thead>
<tbody id="table_body">
</tbody>
</table>
<button class="btn btn-block btn-info" id="process">Process</button>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
<div class="container">
</div>
<br>
<br>
<script>
var vertices = 0;
var array = [];
var graph = [];
$("#button").on('click', function (create_matrix) {
remove_old_table();
var th = document.createElement("th");
vertices = document.getElementById("vertices").value;
th.innerHTML = "#";
$("#table_headers").append(th);
for (let index = 0; index < vertices; index++) {
var header = document.createElement("th");
header.innerHTML = index;
$("#table_headers").append(header);
var row = document.createElement("tr");
th = document.createElement("th");
th.innerHTML = index;
row.append(th);
for (let i = 0; i < vertices; i++) {
td = document.createElement("td");
if (!(index == i))
td.innerHTML = "<input placeholder=\"Value\" type=\"Number\" id=\"" + index + "" + i + "\">"
else
td.innerHTML = "<input value=0 type=\"Number\" disabled id=\"" + index + "" + i + "\">"
row.append(td);
}
$("#table_body").append(row);
}
$("#table").css("visibility", "visible");
})
function remove_old_table() {
$("#table_body").html("");
$("#table_headers").html("");
}
$("#process").on('click', function (process) {
array = [];
graph = [];
for (let i = 0; i < vertices; i++) {
for (let j = 0; j < vertices; j++) {
index = "" + i + "" + j;
array.push(document.getElementById(index).value);
}
graph.push(array);
array = [];
}
main(graph, vertices, display_MST);
})
function display_MST(parent, graph) {
remove_old_table();
var th = document.createElement("th");
th.innerHTML = "#";
$("#table_headers").append(th);
var header1 = document.createElement("th");
header1.innerHTML = "Edge";
var header2 = document.createElement("th");
header2.innerHTML = "Weight";
$("#table_headers").append(header1);
$("#table_headers").append(header2);
for (let index = 1; index < parent.length; index++) {
var row = document.createElement("tr");
th = document.createElement("th");
th.innerHTML = index;
row.append(th);
td = document.createElement("td");
td.innerHTML = parent[index] + ' - ' + index;
row.append(td);
td = document.createElement("td");
td.innerHTML = graph[index][parent[index]];
row.append(td);
$("#table_body").append(row);
}
}
</script>
</body>
</html>