320 likes | 459 Views
Chapter 11. Graphs. Internet Computing Laboratory @ KUT Youn-Hee Han. Where We Are?. Graph. Tree. Binary Tree. AVL Search Tree. Binary Search Tree. Heap. Multiway Trees. 1. Basic Concepts. Graph a collection of nodes (vertices) and lines (edges, arcs) The lines in a graph are called
E N D
Chapter 11. Graphs Internet Computing Laboratory @ KUT Youn-Hee Han
Where We Are? Graph Tree Binary Tree AVL Search Tree Binary Search Tree Heap Multiway Trees
1. Basic Concepts • Graph • a collection of nodes (vertices) and lines (edges, arcs) • The lines in a graph are called • Undirected graphs: edge • Directed graphs (Digraph): arc arc edge
C g c d e A D a b f B 1. Basic Concepts • First use of graphs: Köenigsberg bridge problem [Leonhard Euler 1736] • Starting from land area, is it possible to return to starting location after walking across each of the bridges exactly once? • Eulerian walk is possible if and only if the degree of each vertex is even
1. Basic Concepts • Examples of Graph 0 0 1 2 1 2 3 3 4 5 6 G2 G1 V(G1)={0,1,2,3} E(G1)={(0,1), (0,2), (0,3), (1,2), (1,3), (2,3)} V(G2)={0,1,2,3,4,5,6} E(G2)={(0,1), (0,2), (1,3), (1,4), (2,5), (3,6)}
1. Basic Concepts • Terminologies • Path: sequence of vertices in which each vertex is adjacent to next one • Ex) ABCE, ABEF in Figure (a) • Ex) ABCE, ECBA, ABEF, FEBA in Figure (b) • Two vertices are adjacent (or neighbors) if there is a path connecting them • The vertices share a path of length 1 connecting them. • B is adjacent to A • D is not adjacent to F (a) (b) Data Structure
1. Basic Concepts • Terminologies • Cycle: a path whose start and end vertices are the same • In figure (a), B,C,D,E,B isnot cycle • In figure (b), B,C,D,E,B iscycle • Cyclic Graph is a graph with a cycle • Loop: single arc begins and ends at the same vertex (a) (b) (c) Data Structure
1. Basic Concepts • Terminologies • Two vertices are connected if there is a path between them • A graph is connected if there is a path from any vertex to any other vertex, ignoring direction • Directed graph… • It is strongly connected if there is a path from each vertex to every other vertex, considering direction • Otherwse, it is weakly connected • Undirected graph is always stong connected if it is connected • A graph is disjoint if it is not connected Data Structure
0 1 2 3 1. Basic Concepts • Terminologies • Connected Graph- (a), (b), (d), (e), (f) • Disjoint Graph – (c) • Complete Graph (완전 그래프) – (e), (f) • 모든 Node들 간에 1:1로 직접 연결된 Edge를 지닌 그래프 • 모든 Node들이 서로 인접(Adjacent) 하다. • a graph that has the maximum # of edges (f) Data Structure
1. Basic Concepts • Terminologies • G’(V’,E’) is subgraph of G(V, E) • V(G ’) V(G ) and E(G ’) E(G ) • Subgraph G’는 그래프 G의 일부 Node들과 이들을 연결하는 Edge만을 취하여 만든 부분 그래프 subgraphs of G1 G1 Data Structure
1. Basic Concepts • Terminologies • The degree of a vertex is the number of lines incident to it. • In Figure (a), the degree of vertex B is 3 and the degree of vertex E is 4 • The outdgree and indegree of a vertex • In Figure (a), the indegree of vertex B is 1 and its outdegree is 2 • In Figure (b), the indegree of vertex E is 3 and its outdegree is 1 Data Structure
1. Basic Concepts • Terminologies • 그래프의 동일성 • 아래 두 그래프는 동일하다 (or 일치한다) = homogeneous • Node들 상호간의 공간적인 위치 관계 (Topology)는 중요하지 않다 • 같은 Node 집합을 지니고 있고 그들 사이에 Edge 연결 상태가 동일하면 두 그래프는 일치한다. Data Structure
1. Basic Concepts • 그래프와 트리 • Tree • 연결된 (Connected)& 사이클 없는 (Acyclic) 그래프 • V 개의 정점, 항상 V-1개의 간선 • a graph in which any two vertices are connected by exactly one path Data Structure
1. Basic Concepts • 그래프와 트리 • V 개의 Node들에 대하여 • V-1 개 보다 많은 Edge가 존재하면 사이클이 존재 (Cyclic Graph) • V-1 보다 적으면 절단 그래프 (Disjoint Graph) Data Structure
2. Operations • Inserting a vertex • Deleting a vertex Data Structure
2. Operations • Inserting an edge • Deleting an edge Data Structure
2. Operations • Traverse • Depth-first traversal – Goal Seeking • process all of a vertex’s descendents before we move to an adjacent vertex • Use Stack!!! Data Structure
2. Operations • Traverse • Depth-first traversal – Just Traverse All Vertex • process all of a vertex’s descendents before we move to an adjacent vertex • Use Stack!!! (After Push…, Pop and Process) Data Structure
2. Operations • Traverse • Breadth-first traversal – Just Traverse All Vertex • process all adjacent vertices of a vertex before going to the next level • Use Queue!!! Data Structure
3. Graph Storage Structures • Adjacency Matrix (인접 행렬) • A vector (one-dimensional array) for a vertices and a matrix to store the edges • 2차원 행렬 : 2차원 배열 - A[MAX][MAX] • 직접 연결된 간선이 있으면 해당 값을 1(True) 일종의 심볼 테이블 Data Structure
3. Graph Storage Structures • Adjacency Matrix (인접 행렬) • 무방향 그래프 (Undirected Graph) 인 경우 • 무방향 그래프의 인접행렬은 대각선을 중심으로 대칭 • 메모리 절약을 위해 배열의 반쪽 만을 사용할 수 있음 • 방향 그래프(Directed Graph)인 경우 • 대칭이 아님 일종의 심볼 테이블 Data Structure
3. Graph Storage Structures • Adjacency Matrix (인접 행렬) • 인접행렬 표현을 위한 심볼 테이블 • Node ID를 배열 인덱스로 매핑 시키는 테이블 • “a에서 c로 가는 Direct Edge가 있는가?” • a 0 • e 4 • A[0][4] 가 1 이면 a와 e는 edge 존재 Data Structure
3. Graph Storage Structures • Adjacency List (인접 리스트) • 2-dimensional ragged (울퉁불퉁한) array to store the edges • Vertext list • A singly linked list of the vertices in the list. • 하나의 정점에 인접한 모든 노드를 연결 리스트 형태로 표시 • 연결 리스트를 가리키는 포인터 배열 • 경로에 관한 정보가 아님. • c를 나타내는 A[2] 에 대한 연결 리스트가 deb라고 해서 경로가 그렇다는 것은 아님 Data Structure • Vertext list
3. Graph Storage Structures • Adjacency List (인접 리스트) Data Structure
3. Graph Storage Structures • Adjacency List (인접 리스트) • 무방향 그래프 (Undirected Graph) 인 경우 • 하나의 간선에 대해 두 개의 노드가 나타남. • 인접 리스트의 노드 수는 간선 수의 2배 • 방향 그래프(Directed Graph)인 경우 • 하나의 간선이 정확히 한번 나타남 Data Structure
3. Graph Storage Structures • Adjacency Matrix vs. Adjacency List • 정점 i와 정점 j가 인접해 있는가의 판단 • 인접행렬 (Adjacent Matrix)가 유리 • 정점 i 에 인접한 모든 노드를 찾아라에 대한 연산 • 인접 리스트 (Adjacent List)가 유리 • 공간 면에서 인접행렬은 2V 개의 공간, 인접 리스트는 2E 개의 공간이 필요 • 희소 그래프, 조밀 그래프 • 간선 수가 적은 그래프를 희소 그래프(稀少, Sparse Graph) • 간선 수가 많은 그래프를 조밀 그래프(稠密, Dense Graph) • 희소 그래프일 수록 인접 리스트가 유리 Data Structure
3. Graph Storage Structures • Weighted graph (가중치그래프) • Network 이라고도 함 • a graph whose edges are weighted • 용도: 정점 사이를 이동하는데 필요한 비용이나 거리를 알아봄 Data Structure
3. Graph Storage Structures • Representation of Weighted Graph
3. Graph Storage Structures • Spanning Tree • any tree that consists solely of edges in G that includes all the vertices in G • Spanning tree G’ is a minimal subgraph of G such that • V(G’) = V(G) and G’ is connected • Any connected graph with n vertices must have n-1 edges. • When a number of vertex is n, all connected graphs with n-1 edges are trees G Spanning Trees of G
3. Graph Storage Structures • Minimum (Cost) Spanning Tree • Spanning tree of least cost (sum of weights) • Every vertices are included • Total edge weight is minimum possible • Minimum (Cost) Spanning Tree Algorithms • Kruskal’s algorithm • Prim’s algorithm • Sollin’s algorithm Study in Algorithm Class!!!!!
기말고사 • 일시: 6월 19일 오후 7시 • 장소: B동 315 • 시험 범위: 6장 ~ 11장 Data Structure