Data Journey

International Studies Grad. racing for AI Engineer

Download as .zip Download as .tar.gz View on GitHub
20 December 2020

Non-Linear Data Structure: Graph

by mervyn

Graph

A graph is a data type for storing adjacency relations.

Undirected Graphs

We define an undirected graph as a collection of vertices \(V = \{v_{1}, v_{2}, ..., v_{n}\}\)

\[| E |\leq \binom{| V |}{2}= \frac{| V | ( | V |-1 )}{2}=O ( | V |^{2} )\]

Example 01

Consider this collection of vertices \(V = {v_{1}, v_{2}, ..., v_{9}}\) where \(\lvert V\rvert\) = 9

edges

Associated with these vertices are \(\lvert E \rvert\)=5 edges

\[E = \{ \{ v_{1}, v_{2}\},\{ v_{3}, v_{5}\},\{ v_{4}, v_{8}\},\{ v_{4}, v_{9}\},\{ v_{6}, v_{9}\}\}\]

Example 02

Given the \(\lvert V\rvert\)=n vertices A, B, C, D, E, F, G and the \(\lvert E\rvert\)=9 edges

exm2

Degree

The degree of a vertex is defined as the number of adjacent vertices

degree(A) = degree(D) = degree(C) = 3
degree(B) = degree(E) = 4
degree(F) = 1 degree(G) = 0

Sub-Graphs

A sub-graph of a graph: a subset of the vertices and a subset of the edges that connected the subset of vertices in the original graph

subgraph

Paths

A path in an undirected graph is an ordered sequence of vertices \(( v_{1}, v_{2}, ..., v_{k})\)

Where \({ v_{j-1}, v_{j}}\) is an edge for j = 1, …, k

Example

graphpath

graphpath2

trivialpath

Simple Paths

Connectedness

connected

Weighted Graphs

weight

Trees

A graph is a tree if it is connected and there is a unique path between any two vertices

treegraph

Forests

A forest is any graph that has no cycles

Here is a forest with 22 vertices and 18 edges

forest

Directed Graphs

In a directed graph, the edges on a graph are be associated with a direction

Example

directed

\[E = \{ \{ v_{1}, v_{2}\},\{ v_{3}, v_{5}\},\{ v_{5}, v_{3}\}, \{ v_{6}, v_{9}\},\{ v_{8}, v_{4}\},\{ v_{9}, v_{4}\}\}\]

In and Out Degrees

The degree of a vertex must be modified to consider both cases:

in_degree(\(v_{1}\)) = 0 out_degree(\(v_{1}\)) = 1
in_degree(\(v_{5}\)) = 1 out_degree(\(v_{5}\)) = 1

Sources and Sinks

Sources and Sinks

Paths

unweighteddirected

Connectedness

Two vertices \(v_{j}\), \(v_{k}\) are said to be connected if there exists a path from \(v_{j}\) to \(v_{k}\)

Weighted Directed Graphs

weightdirect

In a weighted directed graphs, each edges is associated with a value

Directed Acyclic Graphs (DAG)

A directed acyclic graph is a directed graph which has no cycles

DAG

Representations

How do we store the adjacency relations?

Binary-Relation List

Adjacency Matrix

adjmatrix

Adjacency List

adjlist

source “K-MOOC 허재필 교수님의 <인공지능을 위한 알고리즘과 자료구조: 이론, 코딩, 그리고 컴퓨팅 사고> 강좌의 3-2 강좌의 그래프 자료구조 중(http://www.kmooc.kr/courses/course-v1:SKKUk+SKKU_46+2020_T1)”

back next

tags:

Comments

Post comment
Loading...

Share this: