Download presentation
Presentation is loading. Please wait.
1
Graphs & Recursion Chapter 2 Highlights
2
Graphs Used to model relationships Includes –Set of vertices (nodes) –Set of edges that connect the nodes Example –V = {A, B, C, D, E} –E = {(A,B), (A,C), (A,E), (B,C), (B,D), (C,E)}
3
Graphs Example –V = {A, B, C, D, E} –E = {(A,B), (A,C), (A,E), (B,C), (B,D), (C,E)} AB D C E
4
Adjacency Matrix 5 X 5 Matrix AB D C E ABCDE AX1101 B1X110 C11X01 D010X0 E1010X
5
Isomorphism Are these graphs the same? A C D BE AB D C E
6
Isomorphism Worse yet! Are these graphs the same?
7
Isomorphism Q: Why is the problem so interesting? A: 1.Two graphs are the input to the problem: How do you define the size of the input? 2.The naive algorithm O(n!) work. 3.The naive algorithm is a crazy recursive algorithm, unlike anything we’ve seen so far.
8
Graphs: Input Size The size of a graph depends on the number of vertices |V| and the number of edges |E|. A graph could have N vertices an no edges. Can a graph have N vertices and 2N edges? Can a graph have N vertices and N 2 edges?
9
Edges Bounded by Vertices N = number of vertices E = number of edges 0 E N(N-1)/2 E N(N-1)/2 E = O(N 2 )
10
Isomorphism: Worst Case In the worst case, a graph with N vertices can have O(N 2 ) edges. To compare two graphs for Isomorphism we must compare all the edges to see if they are equal. ABCDE AX1101 B1X110 C11X01 D010X0 E1010X VWXYZ VX1101 W1X110 X11X01 Y010X0 Z1010X
11
Isomorphism: Worst Case compare(g1, g2) { for (x = 1 to n-1) for (y = x+1 to n-1) if(g1[x][y] != g2[x][y]) return false return true; } ABCDE AX1101 B1X110 C11X01 D010X0 E1010X VWXYZ VX1101 W1X110 X11X01 Y010X0 Z1010X O(n 2 )
12
Isomorphism: Worst Case ? ? ? ? ? AB D C E g1 Not only do we have to compare g1 and g2 O(n 2 )? But, we have to compare g1 with every permutation of g2? g2
13
Isomorphism: Worst Case 4 2 1 3 5 AB D C E g1 Compare g1: {A,B,C,D,E} with g2: {1,2,3,4,5}and g2: {2,1,3,4,5} g2: {2,3,4,1,5} g2
14
Isomorphism fun(Original, Permutation, Target) { if (Original is not empty) for each item in Original remove the item from Original add the item to Permutation fun(Original, Permutation, Target); restore Permutation and Original else if compare(Permutation, Target) return true; return false; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.