Upload
others
View
5
Download
0
Embed Size (px)
Citation preview
Lecture11IntrotoGraphs
ECE241– AdvancedProgrammingIFall2019MikeZink
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
Overview
1
• Introtographs• Abstractdatatype(ADT)• Adjacencymatrix• WordLetterPuzzle• BreadthFirstSearch
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
Objective
• Learnwhatagraphisandhowitisused• Abletoimplementthe graph abstractdatatypeusingmultipleinternalrepresentations
• Toseehowgraphscanbeused tosolveawidevarietyofproblems
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
Graphs• Moregeneralstructurethantrees
• Treeisaspecialtypeofgraph• Graphs canrepresent:
• Roads• Airline flights from city to city• How the Internet is connected
• Oncegoodrepresentation forproblem,graphalgorithmscanbeapplied
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
Graphs• Computer canunderstand roadmapasagraph• Computer canusegraphrepresentation todetermine:
• Shortestpath• Easiestpath• Quickestpath
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
Key: Prerequisite = solid arrow (not vertical) Alternative prereq = dashed arrow Co-requisite = solid vertical arrow Alternative co-req = dashed vertical arrow Required for graduation as CSE = solid outline CSE Senior elective = dashed outline
ECE 211 Circuit
Analysis I
ECE 212 Circuit
Analysis II
CS 121 Intro Problem Solving (Java)
for students who entered CoE before Fall 2016:
for all other students:
ECE 323 Electronics I
ECE 324 Electronics II
ECE 415 Senior Design
Project I
ECE 416 Senior Design
Project II
MATH 131 Calculus I
MATH 132 Calculus II
PHYS 151 Physics I
PHYS 152 Physics II
MATH 235 Linear
Algebra
CS 250 Intro to
Computation
ECE 303 Junior
Seminar
ECE 242 Data
Structures
ECE 232/331 Hardware
Organization
ENGLWRIT 112 College Writing
ENGIN 351 Writing in
Engineering
ENGIN 112 Intro. to ECE
BS-CSE Prerequisite Flowchart, part 1 BS-CSE
ECE 568 Computer
Architecture
ECE 597PD Parallel/Distrib.
Programming
19-Apr-18 1pm
ECE 575 Intro. Analog
IC Design
ECE 373 Software
Engineering
ECE 353 Comp. Systems
Lab I
ECE 597ED Electricity
Infrastructure
(Fall 2018) (Spring 2019)
ECE 354 Comp. Systems
Lab II
ECE 544 Trustworthy Computing
ECE 221/124 Digital
Systems
(Fall 2014 or before)
NOTE: Always check SPIRE for availability, as these offerings can change.
ECE 547 Security
Engineering
ECE 374 Networks & the Internet
ECE 122 Intro
Programming
ECE 597RE Reverse
Engineering
MATH 331 Differential Equations
Graphs
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
Graphs - Definitions• Vertex (node):
• Fundamental part of a graph. • Can have a name =>“key.” • Additional information as “payload.”
• Edge:• Connects two vertices• One-way or two-way• One-way => directed graph (as in example on last slide)
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
Graphs - Definitions• Weight:
• Edges may be weighted• Cost to go from on vertex to another• E.g., cost on edges of graph of roads might represent
distance between two cities
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
Formally Define Graph
8
• Graphcanberepresented byG,whereG=(V,E)• V isasetofvertices, andE isasetofedges• Eachedgeisasetoftupleswherew,v∈ V• Weightcanbeaddedasthirdcomponent oftuple• Asubgraph isasetofedgeseandverticesvsuchthate⊂ Eandv⊂ V
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
Graph Example
9
𝑉 = 𝑉0, 𝑉1,𝑉2, 𝑉3, 𝑉4,𝑉5
𝐸 = 𝑣0, 𝑣1, 5 , 𝑣1, 𝑣2, 0 , 𝑣2,𝑣3,9 , 𝑣3, 𝑣4,7 , 𝑣4, 𝑣0, 1𝑣0,𝑣5, 2 , 𝑣5, 𝑣4, 8 , 𝑣3, 𝑣5,3 , 𝑣5,𝑣2,1
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
Graphs - Definitions• Path:
• w1, w2, …, wn, such that (wi, wi+1) ∈ E for all 1 ≤ i ≤ n-1• Path length = number of edges in path => n-1
• Cycle:• Path that starts and ends at same vertex• (V5, V2, V3, V5) • Acyclic graph => no cycles• Directed acyclic graph (DAG) => directed graph with no
cycles
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
Graphs – Abstract Data Type• Graph()createsnewemptygraph• addVertex(vert) addsaninstanceofVertex tothe
graph• addEdge(fromVert, toVert) addsanew,directed
edgetothegraphthatconnectstwovertices• addEdge(fromVert, toVert, weight) addsa
new,weighted,directededgetothegraphthatconnectstwovertices
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
Graphs – Abstract Data Type• getVertex(vertKey)finds thevertexinthegraph
named vertKey• getVertices() returnsthelistofallverticesinthegraph• in returns True forastatementofthe
form vertex in graph, ifthegivenvertexisinthegraph, False otherwise
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
Graphs – Adjacency Matrix
V0 V1 V2 V3 V4 V5V0 5 2V1 4V2 9V3 7 3V4 1V5 1 8
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
Graphs – Adjacency Matrix• Forsmallgraphs:
• Easytoseewhichnodesareconnected• Butmatrixissparse (manyemptycells),notefficient
• Adjacencymatrixisgoodfitwhen#edges islarge• Completely filledmatrixwouldrequiretoconnectalledgeswitheachother->doesn’toccuroften
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
Adjacency List
15
• Masterlistofallvertices
• Eachvertexmaintainslistofotherverticesitsconnectedto
• Vertexclassusesdictionaryinsteadoflist
• Keysareverticesandvaluesareweights
• Benefits:• Compactlyrepresent sparsematrix
• Easilyfindlinks
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
Vertex Class• Each Vertex usesdictionarytokeeptrackoftheverticestowhichitisconnected, andtheweightofeachedge
• dictionary iscalled connectedTo• addNeighbor method is used to add a connection from
this vertex to another • getConnections method returns all of the vertices in the
adjacency list• getWeight method returns the weight of the edge
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
Vertex Class
17
class Vertex:def __init__(self,key):
self.id = key self.connectedTo = {}
def addNeighbor(self,nbr,weight=0):self.connectedTo[nbr] = weight
def __str__(self):return str(self.id) + ' connectedTo: ' + str([x.id for x in
self.connectedTo])
def getConnections(self):return self.connectedTo.keys()
def getId(self):return self.id
def getWeight(self,nbr):return self.connectedTo[nbr]
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
Graphs Class• Containsdictionarythatmapsvertexnamestovertexobjects
• Classalsoprovidesmethods foraddingandconnectingvertices
• getVertices returnsnamesofallvertices ingraph• __iter__ allowsiterationoverparticulargraph
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
Graph Class
19
class Graph:def __init__(self):
self.vertList = {} self.numVertices = 0
def addVertex(self,key):self.numVertices = self.numVertices + 1newVertex = Vertex(key)self.vertList[key] = newVertexreturn newVertex
def getVertex(self,n):if n in self.vertList:
return self.vertList[n] else: return None
def __contains__(self,n):return n in self.vertList
def addEdge(self,f,t,cost=0):if f not in self.vertList:
nv = self.addVertex(f) if t not in self.vertList:
nv = self.addVertex(t) self.vertList[f].addNeighbor(self.vertList[t], cost)
def getVertices(self):return self.vertList.keys()
def __iter__(self):return iter(self.vertList.values())
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
Graph Class - Operations
20
g = Graph() for i in range(6):
g.addVertex(i)g.vertListg.addEdge(0,1,5) g.addEdge(0,5,2) g.addEdge(1,2,4)g.addEdge(2,3,9)g.addEdge(3,4,7)g.addEdge(3,5,3) g.addEdge(4,0,1) g.addEdge(5,4,8) g.addEdge(5,2,1) for v in g:
for w in v.getConnections(): print("( %s , %s )" % (v.getId(), w.getId()))
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
Word Letter Puzzle
21
• Goal:Transformword“FOOL”into”SAGE”• Changeoneletteratatime• Ateachstep:transform onewordintoanother
FOOLPOOLPOLLPOLEPALESALESAGE
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
Word Letter Puzzle
22
• Cansolvethisproblemusingagraphalgorithm• Represent relationshipsbetweenwordsasgraph
• Usebreadthfirstsearchalgorithm• Findsefficientpathfromstartingworktoendingword
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
Word Letter Puzzle
23
• Firstproblem:howtoturnlargecollectionofwordsintoagraph
• Onlyconnectwordsthatdifferbysingleletter• Ifsuchgraphcanbecreated,anypathfromonewordtoanother isasolution
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
Word Letter Puzzle
24
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
Tackling the Problem
25
• Letsassume listofwords,allsamelength1. Startingpoint:Createavertexforeverywordinlist2. Compareallwordswitheachother3. Ifdifferentbyoneletter=>createedgebetween
them
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
Tackling the Problem
26
Analysis:• Assume listof5,110words• Comparingonewordtoeachotheris≈O(n2)• For5,110wordsthatismorethan26million
comparisons
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
Improved Approach
27
• Huge#ofbucket• Eachwith4-letterwordontop• Oneletteriswildcard“_”• Example:“POPE”and”POPS”match“POP_”
• Whenmatchingbucket isfound, addword• Onceallwords inrightbucket=>mustbeconnected ingraph
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
Building the Graph
28
from pythonds.graphs import Graph
def buildGraph(wordFile): d = {} g = Graph()
wfile = open(wordFile,'r')# create buckets of words that differ by one letterfor line in wfile:
word = line[:-1] for i in range(len(word)):
bucket = word[:i] + '_' + word[i+1:] if bucket in d: d[bucket].append(word) else: d[bucket] = [word]
# add vertices and edges for words in the same bucketfor bucket in d.keys():
for word1 in d[bucket]: for word2 in d[bucket]: if word1 != word2: g.addEdge(word1,word2)
return g
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
Sparsity of Matrix
29
Analysis:• 5,110four-letterwords• Adjacencymatrixwouldhave5,1102=26,112,100
cells• GraphcreatedbybuildGraph()has 53,286edges• =>Only.2%ofmatrixcellswouldbefilled!
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
Implementing Breadth First Search
30
• BreadthFirstSearch(BFS)isoneoftheeasiestalgorithmstosearchagraph
• GivenagraphGandstartingvertexsBFSexploresedges inthegraphtofindallverticesforwhichthereisapathfroms.
• Note:BFSfindsallverticesatdistancekfroms,beforeanyverticesatdistancek+1
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
Implementing Breadth First Search
31
• TovisualizeBFS,imaginethatitisbuildingonelevelatatime
• BFSaddsallchildrenofthestartingvertex• Thenitbeginstodiscoveranyofthegrandchildren• Tokeeptrackofprogressedgesarecoloredwhitegrayorblack:
• White:undiscoveredvertex• Gray:initiallydiscovered
• Black:vertexiscoloredblackwhencompletelyexpored
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
BFS Example
32
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
BFS Example
33
• Startingwithfool,addallnodesadjacenttoit
• Addedasnewnodestoexpand
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
BFS Example
34
• Removes ”pool” fromfrontofthequeue
• Repeatsprocess for“pool”• When“cool”isexaminedalg.detectsthatitisalreadygrey=>shorterpathtocoolalreadyexists
• “poll”isonlynewnodeadded
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
BFS Example
35
• Nextwordinqueue is”foil”• Onlynewnodethat“foil”canaddis“fail”
• Neitherofnexttwonodesaddanythingnewtoqueueortree
• Figureshowstreeafterexpandingallverticeson2ndlevel
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
BFS Example
36
• FinalBFStreeshown• WithBFStree,canstartatanyvertexandfollowpredecessorarrowsbackto
• Findshortestwordladderfromanywordinthetreebacktothestartingvertex
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
Building the Graph
37
def traverse(y): x = y
while (x.getPred()): print(x.getId()) x = x.getPred()
print(x.getId())
traverse(g.getVertex('sage'))
• Functiontraverse()shows how to follow the predecessor links to print out the word ladder
ECE 241 – Adv. Programming I Fall 2019 © 2019 Mike Zink
Next Steps
38
• NextlectureonTuesday: BreadthFirstSearch,DepthFirstSearch
• HW1dueontonight10/17at11PM
ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink39