UUGraph Algorithm
The UUGraph Algorithm, also known as the undirected unweighted graph algorithm, is a fundamental approach in computer science and graph theory, used for solving problems related to undirected and unweighted graphs. An undirected graph is a data structure where nodes (vertices) are connected by edges without any specific direction, and an unweighted graph means that all the edges have the same weight or cost. The UUGraph algorithm is useful for a wide range of applications, including social network analysis, transportation and logistics optimization, and computer network design.
The UUGraph algorithm is a collection of techniques used to solve various graph-related problems, such as finding the shortest path between two nodes, detecting cycles, and determining if a graph is connected or not. Some popular algorithms used in UUGraph include Depth-First Search (DFS), Breadth-First Search (BFS), and Union-Find. Depth-First Search is a traversal algorithm that explores as far as possible along a branch before backtracking, while Breadth-First Search explores all the neighboring nodes at the present depth before moving on to the nodes at the next depth level. Union-Find, on the other hand, is an efficient algorithm for determining connectivity and partitioning the graph into disjoint sets. The choice of algorithm depends on the specific problem and constraints at hand, but all these techniques share the common goal of efficiently solving problems in undirected unweighted graphs.
/*
* Copyright (c) 2017 Kotlin Algorithm Club
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.algorithmexamples.graphs.undirected.unweighted
import com.algorithmexamples.datastructures.Queue
import com.algorithmexamples.graphs.Graph
class UUGraph(public override val V: Int): Graph {
public override var E: Int = 0
private val adj: Array<Queue<Int>> = Array(V) { Queue<Int>() }
public fun addEdge(v: Int, w: Int) {
adj[v].add(w)
adj[w].add(v)
E++
}
public override fun adjacentVertices(v: Int): Collection<Int> {
return adj[v]
}
public fun degree(v: Int): Int {
return adj[v].size
}
}