SlideShare a Scribd company logo
NTHU-CS                                            NTHU-CS




主題: Eulerian Circuit                                         基礎
  基礎                                                          Eulerian circuit
  應用                                                          Extend to directed graphs
  作業與自我挑戰




                                                         1                                                2




                                            NTHU-CS                                            NTHU-CS




Eulerian circuit                                             Property
給一個 undirected graph (allowing multiple edges),一筆劃
走完該 graph 所有 edges 的走法稱為 Eulerian path                        一個一筆劃走完的路徑,除了起點與終點之外,所有
                                                              點的 degree 必為偶數
一筆劃走完而且回到出發點的走法稱為 Eulerian circuit
          4
                                                              若沒有 odd degree 的 vertex,則由任一點 x 出發可以
                                                              造一條 Eulerian circuit 回到 x

   5             3                                            若有 2 個 odd degree 的 vertices x 和 y,則可以造一條
                     1325435121 is an Eulerian circuit        由 x 到 y 的 Eulerian path


   1             2
                                                         3                                                4
NTHU-CS                                                                 NTHU-CS




    Handling multiple edges                                    4
                                                                                   Circuit Construction
    Method 1. use adjacency-matrix                                               Step 1. Find small cycles one by one
      A[1, 2] = 2                                   5                    3       Step 2. Merge small cycles into one
                                                                                               4                                1
                                                                                                                                                 4
                                                                                       5                3               2              5
    Method 2. use adjacency-lists                   1                    2
                                                                                                                                                 3

                 2, 2            3, 1               5, 1                                                           1
      1                                                                                1                2
                                                                                                                                       2
                                                                                                                            3
                                                                                 1213253451 is an Eulerian circuit
                                                                             5                                                                   6




                                                               NTHU-CS                                                                 NTHU-CS




   How to do merging efficiently?                                                  Pseudo code
                                                                                 void find_circuit(int i)
Perform DFS on “edges”
                                   push 1           push 1                       { int j;
Use a stack S to keep the path                  1                    push 4
                                                                                   if (degree[i] == 0) { push(S, i); return; }
                                                           push 5     4
                           push 2                                                   for (j = 0; j < n; j++) //檢查所有的 neighbors,
                                     2                     5                        { if (A[i][j] > 0) { // 至少存在一條 edge (i, j)
                                                                                           A[i][j]--, A[j][i]--; // 拿掉一根 (i, j) (j, i) edge
                                                                      3                    degree[i]--, degree[j]--; // degree 減 1
                        push 1 1                               push 5 push 3               find_circuit(j); }
                                                                                    }
                                                           2
                                                                                   push(S, i);
                                            3              push 2
                                 push 3                                          }
                                                                             7                                                                   8
NTHU-CS                                                         NTHU-CS




Solution                                                      Extend to directed graphs
Assume that an adjacency matrix is used.
                                                                存在 Eulerian path
                                                                 只有一個點的 out-degree 比 in-degree 大 1 (出發點)
Step 1. 判斷 graph 是否相連: O(n2)
                                                                 且只有一個點的 in-degree 比 out-degree 大 1 (終點)
   DFS

                                                                存在 Eulerian circuit
Step 2. 為每個點計算 degree 數: O(n2)
                                                                 所有點的 in-degree 與 out-degree 皆相同
                                                                 由任一點 x 出發可以造一條 Eulerian circuit 回到 x
Step 3. find_circuit: O(mn)

                                                                modification of find-circuit
Time: O(mn)
                                                                  while going from i to j, only an edge (i, j) is removed
   can be improved into O(m) by using adjacency-lists
                                                          9                                                                 10




                                               NTHU-CS                                                         NTHU-CS




應用                                                            應用一: A.302 John’s trip
  應用一: A.302 John’s trip                                      給一個 undirected graph ,找一個 Eulerian circuit
  應用二: A.10054 The Necklace
                                                              當存在不只一個 circuit 時,輸出 lexicographical order
                                                              最小的一個
                                                               如: 1213254351 < 1325435121

                                                              |V| ≤ 44, |E| ≤ 1995




                                                         11                                                                 12
NTHU-CS                                                               NTHU-CS




Solution                                                Example
                                                                                 4
Problem: How to find the smallest one?

   由 i 向外走時,id 最小的優先                                                   5                   3
     adjacency matrix
         for j=0, 1, ..., n-1
     adjacency-lists
         list 中的 nodes 按 id 大小順序排列                                     1                   2


   Start form vertex 1                                     1213253451 is the smallest Eulerian circuit

                                                   13                                                                    14




                                         NTHU-CS                                                               NTHU-CS




應用二: A.10054 The Necklace                               Example
  有 n 個長橢圓珠,每顆珠子的兩半端各有一個顏色
                                                         (Red, Green) × 3
                                                         (Red, Blue) × 3
                                                         (Blue, Green) × 1


  請問,是否可以把這些珠子串成一條項鍊,這條項鍊                                                        G R       R G       G R
  須滿足「相鄰兩棵珠子接觸的兩端顏色相同」
                                                                             G                             R
  5 ≤ n ≤ 1000                                                               B                             B
  1 ≤ color ≤ 50,                                                                    B R       R B

                                                   15                                                                    16
NTHU-CS                                                           NTHU-CS




  Solution                                                     作業與自我挑戰
Example:    (R, G)×3, (R, B)×3, (B, G)×1                       作業
                                                                 練習題
                                Red                                 A.302 John’s Trip http://acm.uva.es/p/v3/302.html
                                                                 挑戰題
                                                                    A.10248 The Integer All-time Champ
                                                                    http://acm.uva.es/p/v102/10248.html
    an undirected graph G                                      其它有趣的題目
                                                 Green           A.10506 Ouroboros
                                                                    http://acm.uva.es/p/v105/10506.html
                                                                 A.10129 Play on Words
                               Blue                                 http://acm.uva.es/p/v101/10129
                                                                 A.10596 Morning Walk
 A Eulerian circuit:                                                http://acm.uva.es/p/v105/10596
   (B, G)(G, R)(R, G)(G, R)(R, B)(B, R)(R, B)
                                                          17                                                                18

More Related Content

Recently uploaded

REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdfREASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
giancarloi8888
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
TechSoup
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
Nguyen Thanh Tu Collection
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
Standardized tool for Intelligence test.
Standardized tool for Intelligence test.Standardized tool for Intelligence test.
Standardized tool for Intelligence test.
deepaannamalai16
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
Himanshu Rai
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
indexPub
 
Data Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsxData Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsx
Prof. Dr. K. Adisesha
 
Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
Iris Thiele Isip-Tan
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
Krassimira Luka
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
Nguyen Thanh Tu Collection
 
The basics of sentences session 7pptx.pptx
The basics of sentences session 7pptx.pptxThe basics of sentences session 7pptx.pptx
The basics of sentences session 7pptx.pptx
heathfieldcps1
 
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptxBIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
RidwanHassanYusuf
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
iammrhaywood
 
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
EduSkills OECD
 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Henry Hollis
 

Recently uploaded (20)

REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdfREASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
Standardized tool for Intelligence test.
Standardized tool for Intelligence test.Standardized tool for Intelligence test.
Standardized tool for Intelligence test.
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
 
Data Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsxData Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsx
 
Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
 
The basics of sentences session 7pptx.pptx
The basics of sentences session 7pptx.pptxThe basics of sentences session 7pptx.pptx
The basics of sentences session 7pptx.pptx
 
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptxBIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
 
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
 

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

T07 Euler Path

  • 1. NTHU-CS NTHU-CS 主題: Eulerian Circuit 基礎 基礎 Eulerian circuit 應用 Extend to directed graphs 作業與自我挑戰 1 2 NTHU-CS NTHU-CS Eulerian circuit Property 給一個 undirected graph (allowing multiple edges),一筆劃 走完該 graph 所有 edges 的走法稱為 Eulerian path 一個一筆劃走完的路徑,除了起點與終點之外,所有 點的 degree 必為偶數 一筆劃走完而且回到出發點的走法稱為 Eulerian circuit 4 若沒有 odd degree 的 vertex,則由任一點 x 出發可以 造一條 Eulerian circuit 回到 x 5 3 若有 2 個 odd degree 的 vertices x 和 y,則可以造一條 1325435121 is an Eulerian circuit 由 x 到 y 的 Eulerian path 1 2 3 4
  • 2. NTHU-CS NTHU-CS Handling multiple edges 4 Circuit Construction Method 1. use adjacency-matrix Step 1. Find small cycles one by one A[1, 2] = 2 5 3 Step 2. Merge small cycles into one 4 1 4 5 3 2 5 Method 2. use adjacency-lists 1 2 3 2, 2 3, 1 5, 1 1 1 1 2 2 3 1213253451 is an Eulerian circuit 5 6 NTHU-CS NTHU-CS How to do merging efficiently? Pseudo code void find_circuit(int i) Perform DFS on “edges” push 1 push 1 { int j; Use a stack S to keep the path 1 push 4 if (degree[i] == 0) { push(S, i); return; } push 5 4 push 2 for (j = 0; j < n; j++) //檢查所有的 neighbors, 2 5 { if (A[i][j] > 0) { // 至少存在一條 edge (i, j) A[i][j]--, A[j][i]--; // 拿掉一根 (i, j) (j, i) edge 3 degree[i]--, degree[j]--; // degree 減 1 push 1 1 push 5 push 3 find_circuit(j); } } 2 push(S, i); 3 push 2 push 3 } 7 8
  • 3. NTHU-CS NTHU-CS Solution Extend to directed graphs Assume that an adjacency matrix is used. 存在 Eulerian path 只有一個點的 out-degree 比 in-degree 大 1 (出發點) Step 1. 判斷 graph 是否相連: O(n2) 且只有一個點的 in-degree 比 out-degree 大 1 (終點) DFS 存在 Eulerian circuit Step 2. 為每個點計算 degree 數: O(n2) 所有點的 in-degree 與 out-degree 皆相同 由任一點 x 出發可以造一條 Eulerian circuit 回到 x Step 3. find_circuit: O(mn) modification of find-circuit Time: O(mn) while going from i to j, only an edge (i, j) is removed can be improved into O(m) by using adjacency-lists 9 10 NTHU-CS NTHU-CS 應用 應用一: A.302 John’s trip 應用一: A.302 John’s trip 給一個 undirected graph ,找一個 Eulerian circuit 應用二: A.10054 The Necklace 當存在不只一個 circuit 時,輸出 lexicographical order 最小的一個 如: 1213254351 < 1325435121 |V| ≤ 44, |E| ≤ 1995 11 12
  • 4. NTHU-CS NTHU-CS Solution Example 4 Problem: How to find the smallest one? 由 i 向外走時,id 最小的優先 5 3 adjacency matrix for j=0, 1, ..., n-1 adjacency-lists list 中的 nodes 按 id 大小順序排列 1 2 Start form vertex 1 1213253451 is the smallest Eulerian circuit 13 14 NTHU-CS NTHU-CS 應用二: A.10054 The Necklace Example 有 n 個長橢圓珠,每顆珠子的兩半端各有一個顏色 (Red, Green) × 3 (Red, Blue) × 3 (Blue, Green) × 1 請問,是否可以把這些珠子串成一條項鍊,這條項鍊 G R R G G R 須滿足「相鄰兩棵珠子接觸的兩端顏色相同」 G R 5 ≤ n ≤ 1000 B B 1 ≤ color ≤ 50, B R R B 15 16
  • 5. NTHU-CS NTHU-CS Solution 作業與自我挑戰 Example: (R, G)×3, (R, B)×3, (B, G)×1 作業 練習題 Red A.302 John’s Trip http://acm.uva.es/p/v3/302.html 挑戰題 A.10248 The Integer All-time Champ http://acm.uva.es/p/v102/10248.html an undirected graph G 其它有趣的題目 Green A.10506 Ouroboros http://acm.uva.es/p/v105/10506.html A.10129 Play on Words Blue http://acm.uva.es/p/v101/10129 A.10596 Morning Walk A Eulerian circuit: http://acm.uva.es/p/v105/10596 (B, G)(G, R)(R, G)(G, R)(R, B)(B, R)(R, B) 17 18