基礎圖論

Basic Graph Theory

講師介紹

  • 劉哲佑
  • WLCSC 25th 社長
  • 高三學測生

TA介紹

  • 李訓佑
  • WLCSC 教學 x 英辯 x 模聯
  • 高三學測生
  • 不知道去文組還理組

What is "Graph" ? 

Grid Graph ?

Grid Graph 

How to present a Grid Graph ?

const int N = 1e3+5;
int Grid[N][N];

Text

Text

Matrix

Traversal

  • DFS

  • BFS

DFS

void DFS(int I,int J){
    // make sure (I,J) is marked visited
    Output(I,J);
    int nxt_i ,nxt_j;
    for(int i=0;i<4;i++){
        nxt_i=I+pos[i][0], nxt_j=J+pos[i][1];
        // check if out of range
        if(I_nxt_i_<1||nxt_i>1000||nxt_j<1||nxt_j>1000){
          continue;
        }
        // check if visited
        if(visited[nxt_i][nxt_j]) continue;
        visited[nxt_i][nxt_j]=true;
        DFS(nxt_i,nxt_j);
    }
}

DFS

上課畫的圖

Small trick

int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};

int cur_r ,cur_c ;
for(int i=0;i<4;i++){
    int nxt_r=cur_r+ dir[i][0] ;
    int nxt_c=cur_c+ dir[i][1] ; 

}

DFS

void DFS(int I,int J){
  // make sure (I,J) is marked visited
  Output(I,J);
  int nxt_i ,nxt_j;
  for(int i=0;i<4;i++){
    nxt_i=I+pos[i][0], nxt_j=J+pos[i][1];
    // check if out of range
    if(I_nxt_i_<1||nxt_i>1000||nxt_j<1||nxt_j>1000) continue;
    // check if visited
    if(visited[nxt_i][nxt_j]) continue;
    visited[nxt_i][nxt_j]=true;
    DFS(nxt_i,nxt_j);
  }
}

BFS

while(q.size()){
  I=q.front().F;
  J=q.front().S;
  q.pop();
  for(int i=0;i<4;i++){
    // next position
    I__=I+pos[i][0], J__=J+pos[i][1];
    if(!inRange(I__,J__)||vis[I__][J__]){
    	continue;
    }
    vis[I__][J__]=true;
    Graph[I__][J__]=Graph[I][J]+1;
    // count steps
    q.push({I__,J__});
  }
}

BFS

上課畫的圖

BFS

while(q.size()){
  I=q.front().F;
  J=q.front().S;
  q.pop();
  for(int i=0;i<4;i++){
    // next position
    I__=I+pos[i][0], J__=J+pos[i][1];
    if(!inRange(I__,J__)||vis[I__][J__]){
    	continue;
    }
    vis[I__][J__]=true;
    Graph[I__][J__]=Graph[I][J]+1;
    // count steps
    q.push({I__,J__});
  }
}

BFS

#define F first
#define S second
typedef pair<int,int> pii;

queue<pii> q;
q.push( {R,C} );

while( q.size() ){ // not empty
	BFS ...

}

Time complexity

每個點都跑過一次

O(RC)

DFS 

ZJ : b701

( ocean or land )

ZJ : a160

( 8 queen problem )

( 應該不屬於圖論但還是放上來ㄌ)

Maze BFS shortest path

ZJ : a982

ZJ : d406

ZJ : b059

(8 direction BFS )

Terms explanation

你怎麼跟別人溝通的??

A : 呀咧呀咧 今天晚上吃什麼 (歪頭

B : 大泰可嗎 wwwww

A : 唔姆.. 可

B : 走 (燦笑

vertex

edge

Text

Undirected

Directed

Text

Unweighted

9

2

1

6

10

4

4

Vertex Weight

Edge Weight

5

10

3

6

7

4

2

2

2

3

2

3

1

1

Degree

In degree

1

1

2

1

1

0

1

Out degree

2

1

0

1

2

1

0

Text

Cycle

Text

Acyclic

Grid Graph ?

  • vertex weight
  • cycle
  • directed or undireted

How to present a Graph ?

  • Adjacency Matrix

  • Adjacency List

4

1

2

3

1 2 3 4
1
2
3
4
0 1 1 0
1 0 1 0
1 1 0 1
0 0 1 0

Undirected

1 2 3 4
1
2
3
4
0 1 1 0
0 0 0 0
0 1 0 1
0 0 0 0

4

1

2

3

Directed

4

1

2

3

1 2 3 4
1
2
3
4
0 5 3 0
5 0 7 0
3 7 0 6
0 0 6 0

Undirected + Weighted

5

7

3

6

1 2 3 4
1
2
3
4
0 5 3 0
0 0 0 0
0 7 0 6
0 0 0 0

4

1

2

3

5

7

3

6

Directed + Weighted

Adj Matrix

int G[N][N];

int u ,v ;
while(m--){
// undirected
    cin>>u>>v;
    G[u][v]=1 ;
    G[v][u]=1 ;
}

Undirected

Adj Matrix

int G[N][N];

int u ,v , wt;
while(m--){
// undirected
    cin>>u>>v>>wt;
    G[u][v]=wt ;
    // G[v][u]=wt ; directed
}

Weighted

Space Complexity

V : 最大節點數量

O (V^2)

Adjacency List 

STL vector

4

1

2

3

1
2
3
4

Undirected

2   3
1   3
1   2   4
3

4

1

2

3

Directed

1
2
3
4
2   3
2   4

4

1

2

3

Undirected + Weighted

5

7

3

6

1
2
3
4
(2,5)   (3,3)
(1,5)   (3,7)
(1,3)   (2,7)   (4,6)
(3,6)

4

1

2

3

5

7

3

6

Directed + Weighted

1
2
3
4
(2,5)   (3,3)
(2,7)   (4,6)

Adj List

vector<int> Graph[N];

inline void Add_vertex_undirected(int a,int b){
    // Undirected graph
    Graph[a].push_back(b);
    Graph[b].push_back(a);
}

Undirected

Adj List

vector<pair<int,int> > Graph[N];

void Add_vertex_ditected(int v,int u,int e){
    //    edge
    // v---------> u
    // vertex v to u with edge= e
    Graph[v].push_back({u,e});
}

Weighted

Adj List

const int MAX_N = 1e5+5;

vector<int> G1[MAX_N];

vector<vector<int> > G2(MAX_N);

vector initialize

Space Complexity

V : 最大節點數量

O (V)

Adj Matrix / List

ZJ : f668

Made with Slides.com