際際滷

際際滷Share a Scribd company logo
SWIFT
Data Structure -
Graph(DFS)
Bill Kim(蟾) | ibillkim@gmail.com
覈谿
DFS
Concept
Examples
Implementation
References
DFS
DFS(Depth-First Search) 蟾 一 朱 蠏碁
覈 蟆暑襯   螻襴讀.
螳譴豺襯 螳讌讌  (覓)覦 蠏碁 覈 蟆暑 蟆曙磯ゼ 蟲
覲  襷  覦.
DFS 蠍磯蓋 企企  碁 語 企 螳ロ 碁襯 
 螳り 伎 螳   蠍語  蟆曙 れ  
襯 碁襦 企螻 蟆郁記 伎 企 碁螳  蟆曙 覈 
 譬襭 覦.
譯朱 蠏 語 伎 ろ  .
Concept
DFS 蠍磯蓋 螻襴讀 襴 れ螻 螳給.
- 蠏 語 覦
1.  碁襯 螻 覦覓誤 蟆朱 
2.  襴ろ碁ゼ  覦一伎 
3.  碁 語 碁 襴ろ碁 覦覲給語 襴磯.
4. 覦覲給  碁螳 覦覓誤  る 蠏襦 DFS 
5. 蠏 DFS    碁  襴ろ 覦一伎 豢螳
6. 語 碁螳 朱  譬襭
Concept
- ろ 覦
1.  碁襯 螻 ろ k.(push)
2. ろ 碁襯  蟶朱碁.(pop)
3. 蟶朱 碁 語 碁螳 讌 誤.
4. 語 碁螳 螻 企 覦覓誤 碁 碁螳 朱 
 碁襯 ろ k.(push)
5. 襷 3覯 伎 語 碁螳 る ろ 碁襯 
蟶朱碁.(pop)
6. れ 3覯 5覯 螻殊 ろ 觜讌 蟾讌 螻 覦覲牛.
Examples
 る 螻襴讀 れ 襯 牛伎 危エ覺.
Examples
D B
A
E
H
F
C
G
DFS 螻襴讀 牛 A 碁 覿一  螻殊 危エ覲企 れ螻 螳給.
Examples
D B
A
E
H
F
C
G
Examples
D B
A
E
H
F
C
G
Examples
D B
A
E
H
F
C
G
Examples
D B
A
E
H
F
C
G
Examples
D B
A
E
H
F
C
G
Examples
D B
A
E
H
F
C
G
Examples
D B
A
E
H
F
C
G
Examples
D B
A
E
H
F
C
G
Examples
D B
A
E
H
F
C
G
Examples
豕譬 蟆暑襯 危エ覲企  螳給.
// node : ["A"]
// neighbors : ["A"]
// node : ["B"]
// neighbors : ["B"]
// node : ["D"]
// neighbors : ["B", "D"]
// node : ["E"]
// neighbors : ["E"]
// node : ["H"]
// neighbors : ["E", "H"]
// node : ["F"]
// neighbors : ["F"]
// node : ["G"]
// neighbors : ["A", "B", "D", "E", "H", "F", "G"]
// node : ["C"]
豕譬 蟆暑 : ["A", "B", "D", "E", "H", "F", "G", "C"]
Examples
るジ  襯 危エ覲願給.
Examples
襷 1覯 碁 豢覦 5覯 碁蟾讌 螳   蟆暑 
 螳給.
1 -> 2 -> 4 -> 5
1 -> 2 -> 5
1 -> 3 -> 2 -> 4 -> 5
1 -> 3 -> 2 -> 5
1 -> 3 -> 4 -> 5
1 -> 4 -> 5
Examples
1覯 碁襯 朱  豕譬 5覯 碁蟾讌 蠏  螻殊
覯 危エ覲願給.
1
語 碁
2 3 4
Examples
1覯 碁襯 朱  豕譬 5覯 碁蟾讌 蠏  螻殊
覯 危エ覲願給.
1
語 碁
2 3 4
1 4 5
Examples
1覯 碁襯 朱  豕譬 5覯 碁蟾讌 蠏  螻殊
覯 危エ覲願給.
1
語 碁
2 3 4
1 4 5
5[ 1, 2, 4, 5 ]
Examples
1覯 碁襯 朱  豕譬 5覯 碁蟾讌 蠏  螻殊
覯 危エ覲願給.
1
語 碁
2 3 4
1 4 5
[ 1, 2, 5 ]
Examples
1覯 碁襯 朱  豕譬 5覯 碁蟾讌 蠏  螻殊
覯 危エ覲願給.
1
語 碁
2 3 4
2 4
1 4 5
Examples
1覯 碁襯 朱  豕譬 5覯 碁蟾讌 蠏  螻殊
覯 危エ覲願給.
1
語 碁
2 3 4
2 4
1 4 5
5
[ 1, 3, 2, 4, 5 ]
Examples
1覯 碁襯 朱  豕譬 5覯 碁蟾讌 蠏  螻殊
覯 危エ覲願給.
1
語 碁
2 3 4
2 4
1 4 5
[ 1, 3, 2, 5 ]
Examples
1覯 碁襯 朱  豕譬 5覯 碁蟾讌 蠏  螻殊
覯 危エ覲願給.
1
語 碁
2 3 4
2 4
5
[ 1, 3, 4, 5 ]
Examples
1覯 碁襯 朱  豕譬 5覯 碁蟾讌 蠏  螻殊
覯 危エ覲願給.
1
語 碁
2 3 4
5
[ 1, 4, 5 ]
Implementation
Swift襯  螳 蠍磯蓋  危エ覲 2螳 襯 讌
 蟲企慨蟆給. 一  螳豌伎 覃  螳給
.
 螳豌
- (Vertex) 螳豌
- 螳(Edge) 螳豌
- 蠏碁(AdjacencyListGraph) 螳豌
蠏碁 蠍磯蓋 覃
- depthFirstSearch : DFS(蟾 一 )襯 ろ
Implementation
public struct Vertex<T>: Equatable where T: Hashable {
public var data: T
public let index: Int
}
extension Vertex: CustomStringConvertible {
public var description: String {
return "(index): (data)"
}
}
extension Vertex: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(data)
hasher.combine(index)
}
}
public func ==<T>(lhs: Vertex<T>, rhs: Vertex<T>) -> Bool {
guard lhs.index == rhs.index else {
return false
}
guard lhs.data == rhs.data else {
return false
}
return true
}
Implementation
public struct Edge<T>: Equatable where T: Hashable {
public let from: Vertex<T>
public let to: Vertex<T>
public let weight: Double?
}
extension Edge: CustomStringConvertible {
public var description: String {
guard let unwrappedWeight = weight else {
return "(from.description) -> (to.description)"
}
return "(from.description) -((unwrappedWeight))-> (to.description)"
}
}
extension Edge: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(from.description)
hasher.combine(to.description)
hasher.combine(weight)
}
}
public func == <T>(lhs: Edge<T>, rhs: Edge<T>) -> Bool {
guard lhs.from == rhs.from else {
return false
}
guard lhs.to == rhs.to else {
return false
}
guard lhs.weight == rhs.weight else {
return false
}
return true
}
Implementation
open class AbstractGraph<T>: CustomStringConvertible where T: Hashable {
public required init() {}
public required init(fromGraph graph: AbstractGraph<T>) {
for edge in graph.edges {
let from = createVertex(edge.from.data)
let to = createVertex(edge.to.data)
addDirectedEdge(from, to: to, withWeight: edge.weight)
}
}
open func createVertex(_ data: T) -> Vertex<T> {
fatalError("abstract function called")
}
open func addDirectedEdge(_ from: Vertex<T>, to: Vertex<T>, withWeight weight: Double?) {
fatalError("abstract function called")
}
open func addUndirectedEdge(_ vertices: (Vertex<T>, Vertex<T>), withWeight weight: Double?) {
fatalError("abstract function called")
}
open func weightFrom(_ sourceVertex: Vertex<T>, to destinationVertex: Vertex<T>) -> Double? {
fatalError("abstract function called")
}
open func edgesFrom(_ sourceVertex: Vertex<T>) -> [Edge<T>] {
fatalError("abstract function called")
}
open var description: String {
fatalError("abstract property accessed")
}
open var vertices: [Vertex<T>] {
fatalError("abstract property accessed")
}
open var edges: [Edge<T>] {
fatalError("abstract property accessed")
}
}
Implementation
private class EdgeList<T> where T: Hashable {
var vertex: Vertex<T>
var edges: [Edge<T>]?
init(vertex: Vertex<T>) {
self.vertex = vertex
}
func addEdge(_ edge: Edge<T>) {
edges?.append(edge)
}
}
Implementation
open class AdjacencyListGraph<T>: AbstractGraph<T> where T: Hashable {
fileprivate var adjacencyList: [EdgeList<T>] = []
public required init() {
super.init()
}
public required init(fromGraph graph: AbstractGraph<T>) {
super.init(fromGraph: graph)
}
open override var vertices: [Vertex<T>] {
var vertices = [Vertex<T>]()
for edgeList in adjacencyList {
vertices.append(edgeList.vertex)
}
return vertices
}
....
Implementation
....
open override var edges: [Edge<T>] {
var allEdges = Set<Edge<T>>()
for edgeList in adjacencyList {
guard let edges = edgeList.edges else {
continue
}
for edge in edges {
allEdges.insert(edge)
}
}
return Array(allEdges)
}
open override func createVertex(_ data: T) -> Vertex<T> {
// check if the vertex already exists
let matchingVertices = vertices.filter { vertex in
return vertex.data == data
}
if matchingVertices.count > 0 {
return matchingVertices.last!
}
// if the vertex doesn't exist, create a new one
let vertex = Vertex(data: data, index: adjacencyList.count)
adjacencyList.append(EdgeList(vertex: vertex))
return vertex
}
.
Implementation
....
open override func addDirectedEdge(_ from: Vertex<T>, to: Vertex<T>, withWeight weight: Double?) {
let edge = Edge(from: from, to: to, weight: weight)
let edgeList = adjacencyList[from.index]
if edgeList.edges != nil {
edgeList.addEdge(edge)
} else {
edgeList.edges = [edge]
}
}
open override func addUndirectedEdge(_ vertices: (Vertex<T>, Vertex<T>), withWeight weight:
Double?) {
addDirectedEdge(vertices.0, to: vertices.1, withWeight: weight)
addDirectedEdge(vertices.1, to: vertices.0, withWeight: weight)
}
open override func weightFrom(_ sourceVertex: Vertex<T>, to destinationVertex: Vertex<T>) ->
Double? {
guard let edges = adjacencyList[sourceVertex.index].edges else {
return nil
}
for edge: Edge<T> in edges {
if edge.to == destinationVertex {
return edge.weight
}
}
return nil
}
.
Implementation
open override func edgesFrom(_ sourceVertex: Vertex<T>) -> [Edge<T>] {
return adjacencyList[sourceVertex.index].edges ?? []
}
open override var description: String {
var rows = [String]()
for edgeList in adjacencyList {
guard let edges = edgeList.edges else {
continue
}
var row = [String]()
for edge in edges {
var value = "(edge.to.data)"
if edge.weight != nil {
value = "((value): (edge.weight!))"
}
row.append(value)
}
rows.append("(edgeList.vertex.data) -> [(row.joined(separator: ", "))]")
}
return rows.joined(separator: "n")
}
}
Implementation
func depthFirstSearch(source: NodeGraph) -> [String] {
var nodesExplored = [source.label]
source.visited = true
for edge in source.neighbors {
if !edge.neighbor.visited {
nodesExplored += depthFirstSearch(source: edge.neighbor)
}
}
return nodesExplored
}
Implementation
let graph = Graph()
let nodeA = graph.addNode("A")
let nodeB = graph.addNode("B")
let nodeC = graph.addNode("C")
let nodeD = graph.addNode("D")
let nodeE = graph.addNode("E")
let nodeF = graph.addNode("F")
let nodeG = graph.addNode("G")
let nodeH = graph.addNode("H")
graph.addEdge(nodeA, neighbor: nodeB)
graph.addEdge(nodeA, neighbor: nodeC)
graph.addEdge(nodeB, neighbor: nodeD)
graph.addEdge(nodeB, neighbor: nodeE)
graph.addEdge(nodeC, neighbor: nodeF)
graph.addEdge(nodeC, neighbor: nodeG)
graph.addEdge(nodeE, neighbor: nodeH)
graph.addEdge(nodeE, neighbor: nodeF)
graph.addEdge(nodeF, neighbor: nodeG)
let nodesExplored = depthFirstSearch(source: nodeA)
print(nodesExplored)
// ["A", "B", "D", "E", "H", "F", "G", "C"]
Implementation
func depthFirstSearch(start: Int, end lastNode: Int, edges: [(Int, Int)]) -> [Any] {
var edgeInfo = [Int: Array<Int>]()
for edge in edges {
if var array = edgeInfo[edge.0] {
array.append(edge.1)
edgeInfo[edge.0] = array
} else { edgeInfo[edge.0] = [edge.1] }
}
var result = 0
var paths:[[Any]] = [[]]
func dfs(node: Int, visited: [Int]) {
guard node != lastNode else {
if result < lastNode { paths.append(visited) }
result += 1
return
}
guard let neighbors = edgeInfo[node] else { return }
for edge in neighbors {
guard visited.contains(edge) == false else { continue }
dfs(node: edge, visited: visited + [edge])
}
}
dfs(node: start, visited: [1])
return paths.filter { $0.count != 0 }
}
Implementation
print(depthFirstSearch(start: 1,
end: 5,
edges: [(1, 2), (1, 3), (1, 4), (2, 1),
(2, 4), (2, 5), (3, 2), (3, 4), (4, 5)]))
// [[1, 2, 4, 5], [1, 2, 5], [1, 3, 2, 4, 5], [1, 3, 2, 5], [1, 3, 4, 5]]
References
[1] Swift襦 蠏碁  螻襴讀 れ 覓語 企慨蠍 -
DFS ク : https://wlaxhrl.tistory.com/88?
category=842165
[2] DFS (Depth-First Search) BFS (Breadth-First Search)
螳 : https://hucet.tistory.com/83
[3] [螻襴讀] DFS & DFS : https://
hyesunzzang.tistory.com/186
[4] 蟾 一  : https://ko.wikipedia.org/wiki/蟾_
_
[5] [Data Structure] 蠏碁 , (DFS) - 襭 蟲譟 :
https://palpit.tistory.com/898
References
[6] DFS (Depth-First Search) BFS (Breadth-First Search) 螳 :
https://hucet.tistory.com/83
[7] Understanding Depth & Breadth-First Search in Swift :
https://medium.com/swift-algorithms-data-structures/
understanding-depth-breadth-first-search-in-
swift-90573fd63a36
[8] Swift) Graph DFS襯 伎 蟆暑 谿剰鍵 : https://velog.io/
@dusdl14/Swift-Graph-DFS襯-伎-蟆暑-谿剰鍵
[9] [螻襴讀] BFS & DFS : https://hyesunzzang.tistory.com/186
[10] 襭蟲譟 :: 蠏碁(2) ", 蟾伎一, 觜一  : http://
egloos.zum.com/printf/v/755736
Thank you!

More Related Content

What's hot (20)

5. queue
5. queue5. queue
5. queue
Geunhyung Kim
=梶 =
=梶 ==梶 =
=梶 =
Kwang Yul Seo
Data Structure - 1st Study
Data Structure - 1st StudyData Structure - 1st Study
Data Structure - 1st Study
Chris Ohk
1.襭蟲譟一 螻襴讀(螳襭)
1.襭蟲譟一 螻襴讀(螳襭)1.襭蟲譟一 螻襴讀(螳襭)
1.襭蟲譟一 螻襴讀(螳襭)
fmbvbfhs
[Swift] Protocol (2/2)
[Swift] Protocol (2/2)[Swift] Protocol (2/2)
[Swift] Protocol (2/2)
Bill Kim
2012 Ds 03
2012 Ds 032012 Ds 03
2012 Ds 03
Jungyerin
Functional programming
Functional programmingFunctional programming
Functional programming
ssuserdcfefa
Python ろ磯
Python ろ磯Python ろ磯
Python ろ磯
sanghyuck Na
R 襦蠏碁覦 蠍磯蓋 覓碁
R 襦蠏碁覦 蠍磯蓋 覓碁R 襦蠏碁覦 蠍磯蓋 覓碁
R 襦蠏碁覦 蠍磯蓋 覓碁
Terry Cho
2012 Ds D2 03 Pdf
2012 Ds D2 03 Pdf2012 Ds D2 03 Pdf
2012 Ds D2 03 Pdf
kd19h
Ch05
Ch05Ch05
Ch05
Hankyo
R ろ磯 る讌
R ろ磯 る讌R ろ磯 る讌
R ろ磯 る讌
Jaeseok Park
覿伎る 覲 覦, From c++98 to c++11, 14
覿伎る 覲 覦, From c++98 to c++11, 14 覿伎る 覲 覦, From c++98 to c++11, 14
覿伎る 覲 覦, From c++98 to c++11, 14
覈 蟾
1.3= = 梶 梶求(higher order procedure) a=梶 =釈メ
1.3= = 梶 梶求(higher order procedure) a=梶 =釈メ1.3= = 梶 梶求(higher order procedure) a=梶 =釈メ
1.3= = 梶 梶求(higher order procedure) a=梶 =釈メ
譴 蟾
R 襦蠏碁 危伎 v1.1
R 襦蠏碁 危伎  v1.1R 襦蠏碁 危伎  v1.1
R 襦蠏碁 危伎 v1.1
happychallenge
(Lisp)
(Lisp)(Lisp)
(Lisp)
EunPyoung Kim
2012 Ds D2 03
2012 Ds D2 032012 Ds D2 03
2012 Ds D2 03
chl132435
Item10. toString 覃 る殊企
Item10. toString 覃  る殊企  Item10. toString 覃  る殊企
Item10. toString 覃 る殊企
Sungho Moon
Data Structure - 1st Study
Data Structure - 1st StudyData Structure - 1st Study
Data Structure - 1st Study
Chris Ohk
1.襭蟲譟一 螻襴讀(螳襭)
1.襭蟲譟一 螻襴讀(螳襭)1.襭蟲譟一 螻襴讀(螳襭)
1.襭蟲譟一 螻襴讀(螳襭)
fmbvbfhs
[Swift] Protocol (2/2)
[Swift] Protocol (2/2)[Swift] Protocol (2/2)
[Swift] Protocol (2/2)
Bill Kim
2012 Ds 03
2012 Ds 032012 Ds 03
2012 Ds 03
Jungyerin
Functional programming
Functional programmingFunctional programming
Functional programming
ssuserdcfefa
R 襦蠏碁覦 蠍磯蓋 覓碁
R 襦蠏碁覦 蠍磯蓋 覓碁R 襦蠏碁覦 蠍磯蓋 覓碁
R 襦蠏碁覦 蠍磯蓋 覓碁
Terry Cho
2012 Ds D2 03 Pdf
2012 Ds D2 03 Pdf2012 Ds D2 03 Pdf
2012 Ds D2 03 Pdf
kd19h
Ch05
Ch05Ch05
Ch05
Hankyo
R ろ磯 る讌
R ろ磯 る讌R ろ磯 る讌
R ろ磯 る讌
Jaeseok Park
覿伎る 覲 覦, From c++98 to c++11, 14
覿伎る 覲 覦, From c++98 to c++11, 14 覿伎る 覲 覦, From c++98 to c++11, 14
覿伎る 覲 覦, From c++98 to c++11, 14
覈 蟾
1.3= = 梶 梶求(higher order procedure) a=梶 =釈メ
1.3= = 梶 梶求(higher order procedure) a=梶 =釈メ1.3= = 梶 梶求(higher order procedure) a=梶 =釈メ
1.3= = 梶 梶求(higher order procedure) a=梶 =釈メ
譴 蟾
R 襦蠏碁 危伎 v1.1
R 襦蠏碁 危伎  v1.1R 襦蠏碁 危伎  v1.1
R 襦蠏碁 危伎 v1.1
happychallenge
2012 Ds D2 03
2012 Ds D2 032012 Ds D2 03
2012 Ds D2 03
chl132435
Item10. toString 覃 る殊企
Item10. toString 覃  る殊企  Item10. toString 覃  る殊企
Item10. toString 覃 る殊企
Sungho Moon

Similar to [Swift] Data Structure - Graph(DFS) (20)

貊 ろ 蟆 蠍 C++ 11 蠏碁 螳 襭 .
貊 ろ 蟆 蠍 C++ 11 蠏碁  螳 襭 .貊 ろ 蟆 蠍 C++ 11 蠏碁  螳 襭 .
貊 ろ 蟆 蠍 C++ 11 蠏碁 螳 襭 .
ultrasuperrok
[Swift] Data Structure - Graph
[Swift] Data Structure - Graph[Swift] Data Structure - Graph
[Swift] Data Structure - Graph
Bill Kim
伎一 Project5
伎一 Project5伎一 Project5
伎一 Project5
KoChungWook
襭蟲譟5覲願
襭蟲譟5覲願襭蟲譟5覲願
襭蟲譟5覲願
KimChangHoen
蠏碁(Graph) 蠏碁 螻襴讀(伎るΜ 覓語 願屋)
蠏碁(Graph) 蠏碁 螻襴讀(伎るΜ 覓語 願屋)蠏碁(Graph) 蠏碁 螻襴讀(伎るΜ 覓語 願屋)
蠏碁(Graph) 蠏碁 螻襴讀(伎るΜ 覓語 願屋)
Eunwoo Cho
Go
GoGo
Go
Project#5 豕蟇磯Μ 谿剰鍵 D0 Hwp
Project#5 豕蟇磯Μ 谿剰鍵 D0 HwpProject#5 豕蟇磯Μ 谿剰鍵 D0 Hwp
Project#5 豕蟇磯Μ 谿剰鍵 D0 Hwp
Kimjeongmoo
= メ罪メ =8 =戟=求 求≡メ
= メ罪メ =8 =戟=求   求≡メ= メ罪メ =8 =戟=求   求≡メ
= メ罪メ =8 =戟=求 求≡メ
daewon jeong
Programming Cascading
Programming CascadingProgramming Cascading
Programming Cascading
Taewook Eom
TABLE ACCESS 伎 伎 SQL _Wh oracle
TABLE ACCESS 伎 伎 SQL _Wh oracleTABLE ACCESS 伎 伎 SQL _Wh oracle
TABLE ACCESS 伎 伎 SQL _Wh oracle
螻 2
 螻 2 螻 2
螻 2
HyeonSeok Choi
DP 螻襴讀 覲伎.pdf
DP 螻襴讀  覲伎.pdfDP 螻襴讀  覲伎.pdf
DP 螻襴讀 覲伎.pdf
Ho Jeong Im
2012 Dm C2 05
2012 Dm C2 052012 Dm C2 05
2012 Dm C2 05
seonhyung
襭蟲譟 蠏碁 覲願
襭蟲譟 蠏碁 覲願襭蟲譟 蠏碁 覲願
襭蟲譟 蠏碁 覲願
mil23
Valentine
ValentineValentine
Valentine
Cheolung Choi
[Swift] Data Structure - Heap
[Swift] Data Structure - Heap[Swift] Data Structure - Heap
[Swift] Data Structure - Heap
Bill Kim
貊 ろ 蟆 蠍 C++ 11 蠏碁 螳 襭 .
貊 ろ 蟆 蠍 C++ 11 蠏碁  螳 襭 .貊 ろ 蟆 蠍 C++ 11 蠏碁  螳 襭 .
貊 ろ 蟆 蠍 C++ 11 蠏碁 螳 襭 .
ultrasuperrok
[Swift] Data Structure - Graph
[Swift] Data Structure - Graph[Swift] Data Structure - Graph
[Swift] Data Structure - Graph
Bill Kim
伎一 Project5
伎一 Project5伎一 Project5
伎一 Project5
KoChungWook
襭蟲譟5覲願
襭蟲譟5覲願襭蟲譟5覲願
襭蟲譟5覲願
KimChangHoen
蠏碁(Graph) 蠏碁 螻襴讀(伎るΜ 覓語 願屋)
蠏碁(Graph) 蠏碁 螻襴讀(伎るΜ 覓語 願屋)蠏碁(Graph) 蠏碁 螻襴讀(伎るΜ 覓語 願屋)
蠏碁(Graph) 蠏碁 螻襴讀(伎るΜ 覓語 願屋)
Eunwoo Cho
Go
GoGo
Go
Project#5 豕蟇磯Μ 谿剰鍵 D0 Hwp
Project#5 豕蟇磯Μ 谿剰鍵 D0 HwpProject#5 豕蟇磯Μ 谿剰鍵 D0 Hwp
Project#5 豕蟇磯Μ 谿剰鍵 D0 Hwp
Kimjeongmoo
= メ罪メ =8 =戟=求 求≡メ
= メ罪メ =8 =戟=求   求≡メ= メ罪メ =8 =戟=求   求≡メ
= メ罪メ =8 =戟=求 求≡メ
daewon jeong
Programming Cascading
Programming CascadingProgramming Cascading
Programming Cascading
Taewook Eom
TABLE ACCESS 伎 伎 SQL _Wh oracle
TABLE ACCESS 伎 伎 SQL _Wh oracleTABLE ACCESS 伎 伎 SQL _Wh oracle
TABLE ACCESS 伎 伎 SQL _Wh oracle
DP 螻襴讀 覲伎.pdf
DP 螻襴讀  覲伎.pdfDP 螻襴讀  覲伎.pdf
DP 螻襴讀 覲伎.pdf
Ho Jeong Im
2012 Dm C2 05
2012 Dm C2 052012 Dm C2 05
2012 Dm C2 05
seonhyung
襭蟲譟 蠏碁 覲願
襭蟲譟 蠏碁 覲願襭蟲譟 蠏碁 覲願
襭蟲譟 蠏碁 覲願
mil23
[Swift] Data Structure - Heap
[Swift] Data Structure - Heap[Swift] Data Structure - Heap
[Swift] Data Structure - Heap
Bill Kim

More from Bill Kim (20)

[Algorithm] Sorting Comparison
[Algorithm] Sorting Comparison[Algorithm] Sorting Comparison
[Algorithm] Sorting Comparison
Bill Kim
[Algorithm] Big O Notation
[Algorithm] Big O Notation[Algorithm] Big O Notation
[Algorithm] Big O Notation
Bill Kim
[Algorithm] Shell Sort
[Algorithm] Shell Sort[Algorithm] Shell Sort
[Algorithm] Shell Sort
Bill Kim
[Algorithm] Radix Sort
[Algorithm] Radix Sort[Algorithm] Radix Sort
[Algorithm] Radix Sort
Bill Kim
[Algorithm] Quick Sort
[Algorithm] Quick Sort[Algorithm] Quick Sort
[Algorithm] Quick Sort
Bill Kim
[Algorithm] Heap Sort
[Algorithm] Heap Sort[Algorithm] Heap Sort
[Algorithm] Heap Sort
Bill Kim
[Algorithm] Counting Sort
[Algorithm] Counting Sort[Algorithm] Counting Sort
[Algorithm] Counting Sort
Bill Kim
[Algorithm] Selection Sort
[Algorithm] Selection Sort[Algorithm] Selection Sort
[Algorithm] Selection Sort
Bill Kim
[Algorithm] Merge Sort
[Algorithm] Merge Sort[Algorithm] Merge Sort
[Algorithm] Merge Sort
Bill Kim
[Algorithm] Insertion Sort
[Algorithm] Insertion Sort[Algorithm] Insertion Sort
[Algorithm] Insertion Sort
Bill Kim
[Algorithm] Bubble Sort
[Algorithm] Bubble Sort[Algorithm] Bubble Sort
[Algorithm] Bubble Sort
Bill Kim
[Algorithm] Binary Search
[Algorithm] Binary Search[Algorithm] Binary Search
[Algorithm] Binary Search
Bill Kim
[Algorithm] Recursive(蠏)
[Algorithm] Recursive(蠏)[Algorithm] Recursive(蠏)
[Algorithm] Recursive(蠏)
Bill Kim
[Swift] Data Structure - AVL
[Swift] Data Structure - AVL[Swift] Data Structure - AVL
[Swift] Data Structure - AVL
Bill Kim
[Swift] Data Structure - Binary Search Tree
[Swift] Data Structure - Binary Search Tree[Swift] Data Structure - Binary Search Tree
[Swift] Data Structure - Binary Search Tree
Bill Kim
[Swift] Data Structure - Binary Tree
[Swift] Data Structure - Binary Tree[Swift] Data Structure - Binary Tree
[Swift] Data Structure - Binary Tree
Bill Kim
[Swift] Data Structure - Tree
[Swift] Data Structure - Tree[Swift] Data Structure - Tree
[Swift] Data Structure - Tree
Bill Kim
[Swift] Data Structure - Dequeue
[Swift] Data Structure - Dequeue[Swift] Data Structure - Dequeue
[Swift] Data Structure - Dequeue
Bill Kim
[Swift] Data Structure - Stack
[Swift] Data Structure - Stack[Swift] Data Structure - Stack
[Swift] Data Structure - Stack
Bill Kim
[Swift] Data Structure - Queue
[Swift] Data Structure - Queue[Swift] Data Structure - Queue
[Swift] Data Structure - Queue
Bill Kim
[Algorithm] Sorting Comparison
[Algorithm] Sorting Comparison[Algorithm] Sorting Comparison
[Algorithm] Sorting Comparison
Bill Kim
[Algorithm] Big O Notation
[Algorithm] Big O Notation[Algorithm] Big O Notation
[Algorithm] Big O Notation
Bill Kim
[Algorithm] Shell Sort
[Algorithm] Shell Sort[Algorithm] Shell Sort
[Algorithm] Shell Sort
Bill Kim
[Algorithm] Radix Sort
[Algorithm] Radix Sort[Algorithm] Radix Sort
[Algorithm] Radix Sort
Bill Kim
[Algorithm] Quick Sort
[Algorithm] Quick Sort[Algorithm] Quick Sort
[Algorithm] Quick Sort
Bill Kim
[Algorithm] Heap Sort
[Algorithm] Heap Sort[Algorithm] Heap Sort
[Algorithm] Heap Sort
Bill Kim
[Algorithm] Counting Sort
[Algorithm] Counting Sort[Algorithm] Counting Sort
[Algorithm] Counting Sort
Bill Kim
[Algorithm] Selection Sort
[Algorithm] Selection Sort[Algorithm] Selection Sort
[Algorithm] Selection Sort
Bill Kim
[Algorithm] Merge Sort
[Algorithm] Merge Sort[Algorithm] Merge Sort
[Algorithm] Merge Sort
Bill Kim
[Algorithm] Insertion Sort
[Algorithm] Insertion Sort[Algorithm] Insertion Sort
[Algorithm] Insertion Sort
Bill Kim
[Algorithm] Bubble Sort
[Algorithm] Bubble Sort[Algorithm] Bubble Sort
[Algorithm] Bubble Sort
Bill Kim
[Algorithm] Binary Search
[Algorithm] Binary Search[Algorithm] Binary Search
[Algorithm] Binary Search
Bill Kim
[Algorithm] Recursive(蠏)
[Algorithm] Recursive(蠏)[Algorithm] Recursive(蠏)
[Algorithm] Recursive(蠏)
Bill Kim
[Swift] Data Structure - AVL
[Swift] Data Structure - AVL[Swift] Data Structure - AVL
[Swift] Data Structure - AVL
Bill Kim
[Swift] Data Structure - Binary Search Tree
[Swift] Data Structure - Binary Search Tree[Swift] Data Structure - Binary Search Tree
[Swift] Data Structure - Binary Search Tree
Bill Kim
[Swift] Data Structure - Binary Tree
[Swift] Data Structure - Binary Tree[Swift] Data Structure - Binary Tree
[Swift] Data Structure - Binary Tree
Bill Kim
[Swift] Data Structure - Tree
[Swift] Data Structure - Tree[Swift] Data Structure - Tree
[Swift] Data Structure - Tree
Bill Kim
[Swift] Data Structure - Dequeue
[Swift] Data Structure - Dequeue[Swift] Data Structure - Dequeue
[Swift] Data Structure - Dequeue
Bill Kim
[Swift] Data Structure - Stack
[Swift] Data Structure - Stack[Swift] Data Structure - Stack
[Swift] Data Structure - Stack
Bill Kim
[Swift] Data Structure - Queue
[Swift] Data Structure - Queue[Swift] Data Structure - Queue
[Swift] Data Structure - Queue
Bill Kim

[Swift] Data Structure - Graph(DFS)

  • 1. SWIFT Data Structure - Graph(DFS) Bill Kim(蟾) | ibillkim@gmail.com
  • 3. DFS DFS(Depth-First Search) 蟾 一 朱 蠏碁 覈 蟆暑襯 螻襴讀. 螳譴豺襯 螳讌讌 (覓)覦 蠏碁 覈 蟆暑 蟆曙磯ゼ 蟲 覲 襷 覦. DFS 蠍磯蓋 企企 碁 語 企 螳ロ 碁襯 螳り 伎 螳 蠍語 蟆曙 れ 襯 碁襦 企螻 蟆郁記 伎 企 碁螳 蟆曙 覈 譬襭 覦. 譯朱 蠏 語 伎 ろ .
  • 4. Concept DFS 蠍磯蓋 螻襴讀 襴 れ螻 螳給. - 蠏 語 覦 1. 碁襯 螻 覦覓誤 蟆朱 2. 襴ろ碁ゼ 覦一伎 3. 碁 語 碁 襴ろ碁 覦覲給語 襴磯. 4. 覦覲給 碁螳 覦覓誤 る 蠏襦 DFS 5. 蠏 DFS 碁 襴ろ 覦一伎 豢螳 6. 語 碁螳 朱 譬襭
  • 5. Concept - ろ 覦 1. 碁襯 螻 ろ k.(push) 2. ろ 碁襯 蟶朱碁.(pop) 3. 蟶朱 碁 語 碁螳 讌 誤. 4. 語 碁螳 螻 企 覦覓誤 碁 碁螳 朱 碁襯 ろ k.(push) 5. 襷 3覯 伎 語 碁螳 る ろ 碁襯 蟶朱碁.(pop) 6. れ 3覯 5覯 螻殊 ろ 觜讌 蟾讌 螻 覦覲牛.
  • 6. Examples る 螻襴讀 れ 襯 牛伎 危エ覺.
  • 7. Examples D B A E H F C G DFS 螻襴讀 牛 A 碁 覿一 螻殊 危エ覲企 れ螻 螳給.
  • 17. Examples 豕譬 蟆暑襯 危エ覲企 螳給. // node : ["A"] // neighbors : ["A"] // node : ["B"] // neighbors : ["B"] // node : ["D"] // neighbors : ["B", "D"] // node : ["E"] // neighbors : ["E"] // node : ["H"] // neighbors : ["E", "H"] // node : ["F"] // neighbors : ["F"] // node : ["G"] // neighbors : ["A", "B", "D", "E", "H", "F", "G"] // node : ["C"] 豕譬 蟆暑 : ["A", "B", "D", "E", "H", "F", "G", "C"]
  • 18. Examples るジ 襯 危エ覲願給.
  • 19. Examples 襷 1覯 碁 豢覦 5覯 碁蟾讌 螳 蟆暑 螳給. 1 -> 2 -> 4 -> 5 1 -> 2 -> 5 1 -> 3 -> 2 -> 4 -> 5 1 -> 3 -> 2 -> 5 1 -> 3 -> 4 -> 5 1 -> 4 -> 5
  • 20. Examples 1覯 碁襯 朱 豕譬 5覯 碁蟾讌 蠏 螻殊 覯 危エ覲願給. 1 語 碁 2 3 4
  • 21. Examples 1覯 碁襯 朱 豕譬 5覯 碁蟾讌 蠏 螻殊 覯 危エ覲願給. 1 語 碁 2 3 4 1 4 5
  • 22. Examples 1覯 碁襯 朱 豕譬 5覯 碁蟾讌 蠏 螻殊 覯 危エ覲願給. 1 語 碁 2 3 4 1 4 5 5[ 1, 2, 4, 5 ]
  • 23. Examples 1覯 碁襯 朱 豕譬 5覯 碁蟾讌 蠏 螻殊 覯 危エ覲願給. 1 語 碁 2 3 4 1 4 5 [ 1, 2, 5 ]
  • 24. Examples 1覯 碁襯 朱 豕譬 5覯 碁蟾讌 蠏 螻殊 覯 危エ覲願給. 1 語 碁 2 3 4 2 4 1 4 5
  • 25. Examples 1覯 碁襯 朱 豕譬 5覯 碁蟾讌 蠏 螻殊 覯 危エ覲願給. 1 語 碁 2 3 4 2 4 1 4 5 5 [ 1, 3, 2, 4, 5 ]
  • 26. Examples 1覯 碁襯 朱 豕譬 5覯 碁蟾讌 蠏 螻殊 覯 危エ覲願給. 1 語 碁 2 3 4 2 4 1 4 5 [ 1, 3, 2, 5 ]
  • 27. Examples 1覯 碁襯 朱 豕譬 5覯 碁蟾讌 蠏 螻殊 覯 危エ覲願給. 1 語 碁 2 3 4 2 4 5 [ 1, 3, 4, 5 ]
  • 28. Examples 1覯 碁襯 朱 豕譬 5覯 碁蟾讌 蠏 螻殊 覯 危エ覲願給. 1 語 碁 2 3 4 5 [ 1, 4, 5 ]
  • 29. Implementation Swift襯 螳 蠍磯蓋 危エ覲 2螳 襯 讌 蟲企慨蟆給. 一 螳豌伎 覃 螳給 . 螳豌 - (Vertex) 螳豌 - 螳(Edge) 螳豌 - 蠏碁(AdjacencyListGraph) 螳豌 蠏碁 蠍磯蓋 覃 - depthFirstSearch : DFS(蟾 一 )襯 ろ
  • 30. Implementation public struct Vertex<T>: Equatable where T: Hashable { public var data: T public let index: Int } extension Vertex: CustomStringConvertible { public var description: String { return "(index): (data)" } } extension Vertex: Hashable { public func hash(into hasher: inout Hasher) { hasher.combine(data) hasher.combine(index) } } public func ==<T>(lhs: Vertex<T>, rhs: Vertex<T>) -> Bool { guard lhs.index == rhs.index else { return false } guard lhs.data == rhs.data else { return false } return true }
  • 31. Implementation public struct Edge<T>: Equatable where T: Hashable { public let from: Vertex<T> public let to: Vertex<T> public let weight: Double? } extension Edge: CustomStringConvertible { public var description: String { guard let unwrappedWeight = weight else { return "(from.description) -> (to.description)" } return "(from.description) -((unwrappedWeight))-> (to.description)" } } extension Edge: Hashable { public func hash(into hasher: inout Hasher) { hasher.combine(from.description) hasher.combine(to.description) hasher.combine(weight) } } public func == <T>(lhs: Edge<T>, rhs: Edge<T>) -> Bool { guard lhs.from == rhs.from else { return false } guard lhs.to == rhs.to else { return false } guard lhs.weight == rhs.weight else { return false } return true }
  • 32. Implementation open class AbstractGraph<T>: CustomStringConvertible where T: Hashable { public required init() {} public required init(fromGraph graph: AbstractGraph<T>) { for edge in graph.edges { let from = createVertex(edge.from.data) let to = createVertex(edge.to.data) addDirectedEdge(from, to: to, withWeight: edge.weight) } } open func createVertex(_ data: T) -> Vertex<T> { fatalError("abstract function called") } open func addDirectedEdge(_ from: Vertex<T>, to: Vertex<T>, withWeight weight: Double?) { fatalError("abstract function called") } open func addUndirectedEdge(_ vertices: (Vertex<T>, Vertex<T>), withWeight weight: Double?) { fatalError("abstract function called") } open func weightFrom(_ sourceVertex: Vertex<T>, to destinationVertex: Vertex<T>) -> Double? { fatalError("abstract function called") } open func edgesFrom(_ sourceVertex: Vertex<T>) -> [Edge<T>] { fatalError("abstract function called") } open var description: String { fatalError("abstract property accessed") } open var vertices: [Vertex<T>] { fatalError("abstract property accessed") } open var edges: [Edge<T>] { fatalError("abstract property accessed") } }
  • 33. Implementation private class EdgeList<T> where T: Hashable { var vertex: Vertex<T> var edges: [Edge<T>]? init(vertex: Vertex<T>) { self.vertex = vertex } func addEdge(_ edge: Edge<T>) { edges?.append(edge) } }
  • 34. Implementation open class AdjacencyListGraph<T>: AbstractGraph<T> where T: Hashable { fileprivate var adjacencyList: [EdgeList<T>] = [] public required init() { super.init() } public required init(fromGraph graph: AbstractGraph<T>) { super.init(fromGraph: graph) } open override var vertices: [Vertex<T>] { var vertices = [Vertex<T>]() for edgeList in adjacencyList { vertices.append(edgeList.vertex) } return vertices } ....
  • 35. Implementation .... open override var edges: [Edge<T>] { var allEdges = Set<Edge<T>>() for edgeList in adjacencyList { guard let edges = edgeList.edges else { continue } for edge in edges { allEdges.insert(edge) } } return Array(allEdges) } open override func createVertex(_ data: T) -> Vertex<T> { // check if the vertex already exists let matchingVertices = vertices.filter { vertex in return vertex.data == data } if matchingVertices.count > 0 { return matchingVertices.last! } // if the vertex doesn't exist, create a new one let vertex = Vertex(data: data, index: adjacencyList.count) adjacencyList.append(EdgeList(vertex: vertex)) return vertex } .
  • 36. Implementation .... open override func addDirectedEdge(_ from: Vertex<T>, to: Vertex<T>, withWeight weight: Double?) { let edge = Edge(from: from, to: to, weight: weight) let edgeList = adjacencyList[from.index] if edgeList.edges != nil { edgeList.addEdge(edge) } else { edgeList.edges = [edge] } } open override func addUndirectedEdge(_ vertices: (Vertex<T>, Vertex<T>), withWeight weight: Double?) { addDirectedEdge(vertices.0, to: vertices.1, withWeight: weight) addDirectedEdge(vertices.1, to: vertices.0, withWeight: weight) } open override func weightFrom(_ sourceVertex: Vertex<T>, to destinationVertex: Vertex<T>) -> Double? { guard let edges = adjacencyList[sourceVertex.index].edges else { return nil } for edge: Edge<T> in edges { if edge.to == destinationVertex { return edge.weight } } return nil } .
  • 37. Implementation open override func edgesFrom(_ sourceVertex: Vertex<T>) -> [Edge<T>] { return adjacencyList[sourceVertex.index].edges ?? [] } open override var description: String { var rows = [String]() for edgeList in adjacencyList { guard let edges = edgeList.edges else { continue } var row = [String]() for edge in edges { var value = "(edge.to.data)" if edge.weight != nil { value = "((value): (edge.weight!))" } row.append(value) } rows.append("(edgeList.vertex.data) -> [(row.joined(separator: ", "))]") } return rows.joined(separator: "n") } }
  • 38. Implementation func depthFirstSearch(source: NodeGraph) -> [String] { var nodesExplored = [source.label] source.visited = true for edge in source.neighbors { if !edge.neighbor.visited { nodesExplored += depthFirstSearch(source: edge.neighbor) } } return nodesExplored }
  • 39. Implementation let graph = Graph() let nodeA = graph.addNode("A") let nodeB = graph.addNode("B") let nodeC = graph.addNode("C") let nodeD = graph.addNode("D") let nodeE = graph.addNode("E") let nodeF = graph.addNode("F") let nodeG = graph.addNode("G") let nodeH = graph.addNode("H") graph.addEdge(nodeA, neighbor: nodeB) graph.addEdge(nodeA, neighbor: nodeC) graph.addEdge(nodeB, neighbor: nodeD) graph.addEdge(nodeB, neighbor: nodeE) graph.addEdge(nodeC, neighbor: nodeF) graph.addEdge(nodeC, neighbor: nodeG) graph.addEdge(nodeE, neighbor: nodeH) graph.addEdge(nodeE, neighbor: nodeF) graph.addEdge(nodeF, neighbor: nodeG) let nodesExplored = depthFirstSearch(source: nodeA) print(nodesExplored) // ["A", "B", "D", "E", "H", "F", "G", "C"]
  • 40. Implementation func depthFirstSearch(start: Int, end lastNode: Int, edges: [(Int, Int)]) -> [Any] { var edgeInfo = [Int: Array<Int>]() for edge in edges { if var array = edgeInfo[edge.0] { array.append(edge.1) edgeInfo[edge.0] = array } else { edgeInfo[edge.0] = [edge.1] } } var result = 0 var paths:[[Any]] = [[]] func dfs(node: Int, visited: [Int]) { guard node != lastNode else { if result < lastNode { paths.append(visited) } result += 1 return } guard let neighbors = edgeInfo[node] else { return } for edge in neighbors { guard visited.contains(edge) == false else { continue } dfs(node: edge, visited: visited + [edge]) } } dfs(node: start, visited: [1]) return paths.filter { $0.count != 0 } }
  • 41. Implementation print(depthFirstSearch(start: 1, end: 5, edges: [(1, 2), (1, 3), (1, 4), (2, 1), (2, 4), (2, 5), (3, 2), (3, 4), (4, 5)])) // [[1, 2, 4, 5], [1, 2, 5], [1, 3, 2, 4, 5], [1, 3, 2, 5], [1, 3, 4, 5]]
  • 42. References [1] Swift襦 蠏碁 螻襴讀 れ 覓語 企慨蠍 - DFS ク : https://wlaxhrl.tistory.com/88? category=842165 [2] DFS (Depth-First Search) BFS (Breadth-First Search) 螳 : https://hucet.tistory.com/83 [3] [螻襴讀] DFS & DFS : https:// hyesunzzang.tistory.com/186 [4] 蟾 一 : https://ko.wikipedia.org/wiki/蟾_ _ [5] [Data Structure] 蠏碁 , (DFS) - 襭 蟲譟 : https://palpit.tistory.com/898
  • 43. References [6] DFS (Depth-First Search) BFS (Breadth-First Search) 螳 : https://hucet.tistory.com/83 [7] Understanding Depth & Breadth-First Search in Swift : https://medium.com/swift-algorithms-data-structures/ understanding-depth-breadth-first-search-in- swift-90573fd63a36 [8] Swift) Graph DFS襯 伎 蟆暑 谿剰鍵 : https://velog.io/ @dusdl14/Swift-Graph-DFS襯-伎-蟆暑-谿剰鍵 [9] [螻襴讀] BFS & DFS : https://hyesunzzang.tistory.com/186 [10] 襭蟲譟 :: 蠏碁(2) ", 蟾伎一, 觜一 : http:// egloos.zum.com/printf/v/755736