我试图分析这段代码的时间复杂程度,但是我被卡住了。我是否认为它是O(n ^ 2)时间复杂度,因为有两个for循环,或者它只是O(n),因为第二个for循环并不总是运行? 使用此代码,我必须基于图形
扫描二维数组//adj is edgeMatrix
public int[] getClosenessCentrality(int[][] adj){
int size = adj.length * adj.length;
int[] closeness = new int[size];
for (int vertex = 0; vertex < adj.length; vertex++) {
boolean[] visited = new boolean[size];
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(size);
pq.add(vertex);
while (!pq.isEmpty()) {
int u = pq.remove();
if(!visited[u]) {
visited[u] = true;
for (int i = 0; i < size; i++) {
//Priority Queue speeds up extract-min
if (!visited[i]) {
if (adj[u][i] > 0) {
pq.add(adj[u][i]+1);
}
}
}
closeness[vertex] += 1;
}
}
}
return closeness;
}
答案 0 :(得分:1)
如果n = adj.length
的复杂性为O(n^3 log(n))
。
这就是原因。
n
vertex
n^2
个O(log(n^2)) = O(2 log(n)) = O(log(n))
的不同值:O(n^3)
可能的边缘,我们:O(log(n))
次操作。)所以把它们放在一起,我们O(n^3 log(n))
总共有 let textToType = "This is what I want you to type.";
const typedLetters = [];
document.getElementById('user-input').addEventListener("keypress", function(event) {
const key = event.which || event.keyCode;
const nextLetter = textToType[0].charCodeAt();
const outputTarget = document.getElementById("display-text");
const greenWrapper = document.createElement("span");
greenWrapper.classList.add("typed-cursor");
if (key === nextLetter) {
typedLetters.push(String.fromCharCode(nextLetter));
textToType = textToType.substr(1);
greenWrapper.textContent = typedLetters.join("");
outputTarget.textContent = textToType;
outputTarget.prepend(greenWrapper);
};
})
个body {
font-family: monospace;
}
.title-of-page>h1 {
text-align: center;
font-family: monospace;
}
.title-of-page {
background-color: #414a4c;
color: #ced3db;
}
.jumbotron {
margin: 0;
}
.navigation-bar {
background-color: #46494f;
}
a {
color: green;
}
.nav>li>a:hover {
background-color: #878f9b;
}
.navbar-nav>li {
text-align: center;
float: none;
display: table-cell;
}
.navbar-nav {
display: table;
width: 100%;
margin: 0;
}
.navbar {
margin: 0;
padding: 0;
border-radius: 0;
}
.typing-field {
width: 60em;
height: 8em;
background-color: #7e7e7f;
opacity: 0.4;
margin-left: 15em;
margin-top: 5em;
border: 3px solid black;
padding: 0.8em;
}
#display-text {
color: white;
font-size: 2em;
}
.user-input {
font-size: 1em;
padding-left: 35em;
padding-top: 2em;
}
#timer {
padding-top: 4em;
padding-left: 10em;
font-size: 1.5em;
color: red;
}
.typed-cursor {
opacity: 1;
-webkit-animation: blink 0.7s infinite;
-moz-animation: blink 0.7s infinite;
animation: blink 0.7s infinite;
color: black;
}
@keyframes blink {
0% {
opacity: 1;
}
50% {
opacity: 0;
}
100% {
opacity: 1;
}
}
@-webkit-keyframes blink {
0% {
opacity: 1;
}
50% {
opacity: 0;
}
100% {
opacity: 1;
}
}
@-moz-keyframes blink {
0% {
opacity: 1;
}
50% {
opacity: 0;
}
100% {
opacity: 1;
}
}
可能的复杂 <div class="jumbotron title-of-page container-fluid">
<h1>Typing Counter</h1>
</div>
<nav class="navbar navigation-bar container-fluid">
<div class="">
<ul class="nav navbar-nav">
<li><a href="#">Home</a></li>
<li><a href="#">Contest</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Leaderboard</a></li>
</ul>
</div>
</nav>
<div>
<div id="timer">
<button type="button" class="btn" onclick="timer();">Start</button>
</div>
<div class="typing-field">
<p id="display-text">This is what I want you to type.
</div>
<div class="user-input">
<input type="text" name="user-input-text-box" id="user-input" />
</div>
</div>
操作。