[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"sanity-7W5C7dyeIKi0AGVCKOzAMVUXP6MiUh8CnATtJlIwJM8":3,"sanity-zsJ5bwDMOh6-zTXfF9rtlZdYPl5SSd0K5lGa7m4aKOc":467},{"data":4,"sourceMap":-1},{"latestPodcast":5,"latestReleases":14,"post":39,"recent":442},[6],{"_id":7,"publishedAt":8,"slug":9,"sponsored":12,"title":13},"7ebd76d2-cebc-4b88-82f9-3dfb2c8bc5ca","2026-07-08T04:40:00.000Z",{"_type":10,"current":11},"slug","what-s-left-for-infrastructure-as-code-after-ai-moves-in",true,"What's left for infrastructure-as-code after AI moves in?",[15,21,27,33],{"_id":16,"publishedAt":17,"slug":18,"title":20},"eb5b66eb-9410-4329-83bb-22bbff39402a","2026-04-28T13:00:00.000Z",{"_type":10,"current":19},"turn-scattered-knowledge-into-trusted-intelligence","Turning scattered knowledge into trusted intelligence: Stack Internal 2026.3",{"_id":22,"publishedAt":23,"slug":24,"title":26},"369c2401-b62e-4a37-8ff8-bf603023ecad","2026-03-02T15:03:00.988Z",{"_type":10,"current":25},"what-s-new-at-stack-overflow-march-2026","What’s new at Stack Overflow: March 2026",{"_id":28,"publishedAt":29,"slug":30,"title":32},"5e9053a4-07ea-447c-91ea-29e0b6228537","2026-02-02T15:00:00.000Z",{"_type":10,"current":31},"what-s-new-at-stack-overflow-february-2026","What’s new at Stack Overflow: February 2026",{"_id":34,"publishedAt":35,"slug":36,"title":38},"a1b538eb-a8a6-46d0-80a1-ac70ec9bb935","2026-01-05T10:00:00.000-05:00",{"_type":10,"current":37},"what-s-new-at-stack-overflow-january-2026","What’s new at Stack Overflow: January 2026",{"_createdAt":40,"_id":41,"_rev":42,"_system":43,"_type":46,"_updatedAt":47,"author":48,"body":65,"comments":12,"dateUrl":407,"excerpt":408,"image":409,"legacyBody":412,"product":88,"publishedAt":415,"slug":416,"sponsored":88,"tags":418,"title":441,"visible":12},"2023-05-24T12:51:00Z","wp-post-20158","Bqj3l3jZyXV24sfjnzcKFZ",{"base":44},{"id":41,"rev":45},"0aYkJXBENQTxI4C1z8kiML","blogPost","2026-05-11T18:36:11Z",[49],{"_createdAt":50,"_id":51,"_rev":52,"_type":53,"_updatedAt":54,"avatar":55,"bio":60,"employee":61,"name":62,"slug":63},"2023-05-23T16:27:18Z","wp-author-cap-19500","dgl3SCUzppW3U2LvCoP6c0","blogAuthor","2023-06-20T15:05:12Z",{"_type":56,"asset":57},"image",{"_ref":58,"_type":59},"image-1a35ac25107b0af6163c992f8b6e7c71430227dd-1025x1025-jpg","reference","Wayne is a freelance software engineer, author, and educator. He works with iOS developers to help them pass technical interviews at top companies like Apple, Google, and Facebook. Thousands have used his book, online classes, and YouTube channel to learn new concepts, gain the necessary practice, and connect with fellow developers worldwide. He lives in Seattle, WA.","none","Wayne Bishop",{"current":64},"wayne-bishop",[66,90,99,107,112,129,145,150,170,190,198,206,209,214,219,227,243,248,256,272,277,285,310,313,321,329,337,345,353,356,364,396,399],{"_key":67,"_type":68,"children":69,"markDefs":84,"style":89},"0c2b610415ef","block",[70,75,80],{"_key":71,"_type":72,"marks":73,"text":74},"0c2b610415ef0","span",[],"If you've been programming for long enough, you have heard about the concept of a graph. It’s required content for a degree in computer science, and many top-level companies test for an understanding of graph theory during ",{"_key":76,"_type":72,"marks":77,"text":79},"0c2b610415ef1",[78],"ae5e89481ca0","technical interviews",{"_key":81,"_type":72,"marks":82,"text":83},"0c2b610415ef2",[],". However, you don’t need to be working on advanced problems to utilize the concepts. Let’s review why graphs are popular data structures and how you can implement them in code.",[85],{"_key":78,"_type":86,"href":87,"reference":88},"link","https:\u002F\u002Fwww.waynewbishop.com",null,"normal",{"_key":91,"_type":68,"children":92,"markDefs":97,"style":98},"2c87bce77a8b",[93],{"_key":94,"_type":72,"marks":95,"text":96},"2c87bce77a8b0",[],"Relational models",[],"h2",{"_key":100,"_type":68,"children":101,"markDefs":106,"style":89},"147009cf8e1f",[102],{"_key":103,"_type":72,"marks":104,"text":105},"147009cf8e1f0",[],"Regardless of coding experience, you should be familiar with the data types of arrays and dictionaries. These collections are standard concepts used in most languages and work great when presenting list-based content:",[],{"_key":108,"_type":56,"alt":88,"asset":109,"caption":111,"markDefs":88},"b1e6de50a173",{"_ref":110,"_type":59},"image-f5597b6783f09908452329ad181f6393ff3605d2-462x173-png","",{"_key":113,"_type":68,"children":114,"markDefs":128,"style":89},"b87dcb644ce4",[115,119,124],{"_key":116,"_type":72,"marks":117,"text":118},"b87dcb644ce40",[],"Most times, lists are the perfect solution for presenting information from a database or a REST-based query. However, there are times when a list needs to provide ",{"_key":120,"_type":72,"marks":121,"text":123},"b87dcb644ce41",[122],"strong","context",{"_key":125,"_type":72,"marks":126,"text":127},"b87dcb644ce42",[]," as to how records relate to each other. This is when organizing data as a graph becomes handy.",[],{"_key":130,"_type":68,"children":131,"markDefs":144,"style":89},"887160fb5e15",[132,136,140],{"_key":133,"_type":72,"marks":134,"text":135},"887160fb5e150",[],"With graphs, the primary objective isn't to list information (although entirely possible) but to define the ",{"_key":137,"_type":72,"marks":138,"text":139},"887160fb5e151",[122],"relationship",{"_key":141,"_type":72,"marks":142,"text":143},"887160fb5e152",[]," between objects. Why would that be useful?",[],{"_key":146,"_type":56,"alt":147,"asset":148,"caption":147,"markDefs":88},"a54cebbcb59d","An undirected graph with two vertices and one edge.",{"_ref":149,"_type":59},"image-271d133886c67fb83a35638ec44a2af1e2ea72db-208x63-png",{"_key":151,"_type":68,"children":152,"markDefs":169,"style":89},"c2c0b1a28ce6",[153,157,161,165],{"_key":154,"_type":72,"marks":155,"text":156},"c2c0b1a28ce60",[122],"Mapping applications",{"_key":158,"_type":72,"marks":159,"text":160},"c2c0b1a28ce61",[],": How would you organize data needed to recreate a mapping service like Apple or Google Maps if asked in a technical interview? More than just providing a list of all known roads in a database, your model would need to determine the best way to reach a destination based on the time of day, traffic, and one-way streets among other factors. For this considerable volume of data to be effective, you need to know how a single road ",{"_key":162,"_type":72,"marks":163,"text":164},"c2c0b1a28ce62",[122],"relates",{"_key":166,"_type":72,"marks":167,"text":168},"c2c0b1a28ce63",[]," to all other streets in the model.",[],{"_key":171,"_type":68,"children":172,"markDefs":189,"style":89},"c1b9ca9e7a70",[173,177,181,185],{"_key":174,"_type":72,"marks":175,"text":176},"c1b9ca9e7a700",[122],"Social media:",{"_key":178,"_type":72,"marks":179,"text":180},"c1b9ca9e7a701",[],"Success on social media is often measured by the number of people you follow or that follow you. Networking platforms like Twitter attract massive audiences by allowing you to connect with anyone, permitting you to receive their latest updates. The LinkedIn model is more detailed as you cannot add someone to your network unless the receiver accepts your connection request. In this case, LinkedIn connections represent two-way ",{"_key":182,"_type":72,"marks":183,"text":184},"c1b9ca9e7a702",[122],"relationships",{"_key":186,"_type":72,"marks":187,"text":188},"c1b9ca9e7a703",[],". Taking this idea further, you can also search to see if anyone in your network is connected to a job opportunity you want to pursue. In this case, \"network\" could imply a direct or indirect connection. Such a robust model isn't just based on a simple list but would include the smarts to determine how all profiles relate.",[],{"_key":191,"_type":68,"children":192,"markDefs":197,"style":98},"267815cfa29f",[193],{"_key":194,"_type":72,"marks":195,"text":196},"267815cfa29f0",[],"Graph components",[],{"_key":199,"_type":68,"children":200,"markDefs":205,"style":89},"cc24d43ae578",[201],{"_key":202,"_type":72,"marks":203,"text":204},"cc24d43ae5780",[],"Now that we've seen how graphs are used in everyday applications let's introduce their components. Nodes in a graph are referred to as vertices. While it would be possible to build a graph as a single vertex, models that contain multiple vertices better represent real-world applications. Graph objects relate to one another through connections called edges. Depending on your requirements, a vertex could be connected to one or more things through edges. It's also possible to create a vertex without edges. Finally, unlike other standard structures like stacks or queues, graphs often have no designated start or end point. Here are some example graph configurations:",[],{"_key":207,"_type":56,"alt":147,"asset":208,"caption":147,"markDefs":88},"6f0fa8b04458",{"_ref":149,"_type":59},{"_key":210,"_type":56,"alt":211,"asset":212,"caption":211,"markDefs":88},"4ae6701e2afc","An undirected graph with four vertices and three edges.",{"_ref":213,"_type":59},"image-04511e9ca2aa4203b476b3c464603cad2e646baa-302x165-png",{"_key":215,"_type":56,"alt":216,"asset":217,"caption":216,"markDefs":88},"24b5142acdb1","An undirected graph with three vertices and three edges.",{"_ref":218,"_type":59},"image-a862c9d068c2582172979347e8e32bbcee726323-212x181-png",{"_key":220,"_type":68,"children":221,"markDefs":226,"style":98},"3d43ac958a54",[222],{"_key":223,"_type":72,"marks":224,"text":225},"3d43ac958a540",[],"Directed vs. undirected",[],{"_key":228,"_type":68,"children":229,"markDefs":242,"style":89},"4bc7d9e25137",[230,234,238],{"_key":231,"_type":72,"marks":232,"text":233},"4bc7d9e251370",[],"In an undirected graph, the connection between a source vertex and destination are equal. These models represent two-way connections—similar to a two-way street in the mapping application. To define a connection going a single direction, we can update our model to a ",{"_key":235,"_type":72,"marks":236,"text":237},"4bc7d9e251371",[122],"directed",{"_key":239,"_type":72,"marks":240,"text":241},"4bc7d9e251372",[]," graph by using lines and arrows:",[],{"_key":244,"_type":56,"alt":245,"asset":246,"caption":245,"markDefs":88},"a2885b85c548","A directed graph with three vertices and three edges.",{"_ref":247,"_type":59},"image-fe8d131caf2fd6c1eb0299f34793eb488b722d21-211x181-png",{"_key":249,"_type":68,"children":250,"markDefs":255,"style":98},"cc9b27afda6d",[251],{"_key":252,"_type":72,"marks":253,"text":254},"cc9b27afda6d0",[],"Level of connectedness",[],{"_key":257,"_type":68,"children":258,"markDefs":271,"style":89},"01fecb68b768",[259,263,267],{"_key":260,"_type":72,"marks":261,"text":262},"01fecb68b7680",[],"Sometimes, we must represent the level of connectedness between vertices in a graph. This technique works well when quantifying distances, times, or severity between nodes. Generally associated with an edge, the ",{"_key":264,"_type":72,"marks":265,"text":266},"01fecb68b7681",[122],"weight",{"_key":268,"_type":72,"marks":269,"text":270},"01fecb68b7682",[]," is a comparative variable tracked for this purpose.",[],{"_key":273,"_type":56,"alt":274,"asset":275,"caption":274,"markDefs":88},"5ce960ba6305","A directed graph with three vertices and three edges where the edges are weighted.",{"_ref":276,"_type":59},"image-6a9417fb6f7876aba021a83f05ddb54f80b196b1-212x181-png",{"_key":278,"_type":68,"children":279,"markDefs":284,"style":98},"3f49492a90ce",[280],{"_key":281,"_type":72,"marks":282,"text":283},"3f49492a90ce0",[],"Graph vertex",[],{"_key":286,"_type":68,"children":287,"markDefs":309,"style":89},"ac0e8b886a41",[288,292,297,301,305],{"_key":289,"_type":72,"marks":290,"text":291},"ac0e8b886a410",[],"With a basic understanding of graph theory in place, let's see how to replicate some of these models in code. Below we've created a vertex that supports a custom generic object (",{"_key":293,"_type":72,"marks":294,"text":296},"ac0e8b886a411",[295],"code","T",{"_key":298,"_type":72,"marks":299,"text":300},"ac0e8b886a412",[],"). The ",{"_key":302,"_type":72,"marks":303,"text":304},"ac0e8b886a413",[295],"tvalue",{"_key":306,"_type":72,"marks":307,"text":308},"ac0e8b886a414",[]," variable represents the data held by the type, including a single string, int, or custom type (for example., street name or social media profile). Also, note our class conforms to the popular Equatable protocol (Swift). This will allow us to compare specific vertex instances for equality if required.",[],{"_key":311,"_type":295,"code":312,"markDefs":88},"e23baac82548","public class Vertex \u003CT> : Equatable {\n   \t \n\tvar tvalue: T?\n\tvar neighbors = Array\u003CEdge\u003CT>>()\n\tlet uuid = UUID()\n        \n\tpublic init(with name: T) {\n   \tself.tvalue = name\n\t}\n   \t \n\t\u002F\u002Fequatable conformance\n\tpublic static func == (lhs: Vertex, rhs: Vertex) -> Bool {\n    \treturn lhs.uuid == rhs.uuid\n\t}\n}\n",{"_key":314,"_type":68,"children":315,"markDefs":320,"style":98},"dd70454cff2d",[316],{"_key":317,"_type":72,"marks":318,"text":319},"dd70454cff2d0",[],"Adjacency lists",[],{"_key":322,"_type":68,"children":323,"markDefs":328,"style":89},"c1a36424a6ee",[324],{"_key":325,"_type":72,"marks":326,"text":327},"c1a36424a6ee0",[],"The neighbors property represents the connections made to other vertices. As discussed, each vertex can connect to one or more neighbors. This list of relationships is sometimes called an “adjacency list” and can be used to solve many advanced problems.",[],{"_key":330,"_type":68,"children":331,"markDefs":336,"style":89},"09b65f29a2d5",[332],{"_key":333,"_type":72,"marks":334,"text":335},"09b65f29a2d50",[295],"var neighbors = Array\u003CEdge\u003CT>>()",[],{"_key":338,"_type":68,"children":339,"markDefs":344,"style":98},"34a0e135257e",[340],{"_key":341,"_type":72,"marks":342,"text":343},"34a0e135257e0",[],"Graph edge",[],{"_key":346,"_type":68,"children":347,"markDefs":352,"style":89},"75622a97e57f",[348],{"_key":349,"_type":72,"marks":350,"text":351},"75622a97e57f0",[],"We added a neighbor property to store an array of custom edge types when creating our vertex. Below, an edge provides a reference for a subsequent neighboring vertex and its potential edge weight value.",[],{"_key":354,"_type":295,"code":355,"markDefs":88},"7a75519c2baa","public class Edge \u003CT> {\n    \n\tvar neighbor: Vertex\u003CT>\n\tvar weight: Int\n    \n\tinit() {\n    \tweight = 0\n    \tself.neighbor = Vertex\u003CT>()\n\t}\n}\n",{"_key":357,"_type":68,"children":358,"markDefs":363,"style":98},"cf1fd4928ab0",[359],{"_key":360,"_type":72,"marks":361,"text":362},"cf1fd4928ab00",[],"Building the canvas",[],{"_key":365,"_type":68,"children":366,"markDefs":395,"style":89},"5d0d143c198d",[367,371,375,379,383,387,391],{"_key":368,"_type":72,"marks":369,"text":370},"5d0d143c198d0",[],"With our vertex and edge objects in place, we can now add them to our central storage structure we'll call the graph canvas. Even though our canvas is technically an array, the goal is to ",{"_key":372,"_type":72,"marks":373,"text":374},"5d0d143c198d1",[122],"visualize",{"_key":376,"_type":72,"marks":377,"text":378},"5d0d143c198d2",[]," the collection as a set of relationships. The ",{"_key":380,"_type":72,"marks":381,"text":382},"5d0d143c198d3",[295],"addVertex",{"_key":384,"_type":72,"marks":385,"text":386},"5d0d143c198d4",[]," function allows us to add a single generic vertex to the canvas, while the ",{"_key":388,"_type":72,"marks":389,"text":390},"5d0d143c198d5",[295],"addEdge",{"_key":392,"_type":72,"marks":393,"text":394},"5d0d143c198d6",[]," method provides reference information needed for an edge. Lastly, our code assumes the graph is directed, as the edge is (only) added to the source vertex adjacency list.",[],{"_key":397,"_type":295,"code":398,"markDefs":88},"b2e3069dc89d","public class Graph \u003CT> {\n   \n\tvar canvas: Array\u003CVertex\u003CT>>\n  \t \n   public init() {\n    \tcanvas = Array\u003CVertex>()\n\t}\n    \n\t\u002F\u002Fadd vertex to graph canvas\n\tpublic func addVertex(element: Vertex\u003CT>) {\n    \tcanvas.append(element)\n\t}\n    \n      \u002Fadd edge\n\tpublic func addEdge(source: Vertex\u003CT>, neighbor: Vertex\u003CT>, weight: Int) {\n   \t \n    \t\u002F\u002Fcreate a new edge\n    \tlet newEdge = Edge\u003CT>()\n   \t \n   \t   \u002F\u002Fconnect source vertex to neighboring edge\n    \tnewEdge.neighbor = neighbor\n    \tnewEdge.weight = weight\n   \t\t \n    \tsource.neighbors.append(newEdge)\n\t}\n}\n",{"_key":400,"_type":68,"children":401,"markDefs":406,"style":89},"b18f2326c9a9",[402],{"_key":403,"_type":72,"marks":404,"text":405},"b18f2326c9a90",[],"In summary, we've introduced graphs and have seen how they are used to represent the relationship between objects. We also reviewed a few ways to configure a graph and the components used to describe different models. With our model defined, we've set the stage for more advanced functionality, including graph navigation and traversal algorithms like breadth-first search.",[],"2022\u002F05\u002F26","Lists are useful data structures, but sometimes you need to show the context between items. ",{"_type":56,"asset":410},{"_ref":411,"_type":59},"image-eaf1774fb92f0c7cbd6d0a54fd8241cb7a29ab9b-2560x1344-jpg",{"code":413,"language":414},"\u003C!-- wp:paragraph -->\n\u003Cp>If you've been programming for long enough, you have heard about the concept of a graph. It’s required content for a degree in computer science, and many top-level companies test for an understanding of graph theory during \u003Ca href=\"https:\u002F\u002Fwww.waynewbishop.com\">technical interviews\u003C\u002Fa>. However, you don’t need to be working on advanced problems to utilize the concepts. Let’s review why graphs are popular data structures and how you can implement them in code.&nbsp;\u003C\u002Fp>\n\u003C!-- \u002Fwp:paragraph -->\n\n\u003C!-- wp:heading -->\n\u003Ch2 id=\"h-relational-models\">Relational models\u003C\u002Fh2>\n\u003C!-- \u002Fwp:heading -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>Regardless of coding experience, you should be familiar with the data types of arrays and dictionaries. These collections are standard concepts used in most languages and work great when presenting list-based content:\u003C\u002Fp>\n\u003C!-- \u002Fwp:paragraph -->\n\n\u003C!-- wp:image -->\n\u003Cfigure class=\"wp-block-image\">\u003Cimg src=\"https:\u002F\u002Flh6.googleusercontent.com\u002F6pWpT8GFVHemcWClK8lca9NUSRtYhL7L4wvAGbFauebiQcseJz0vkaboG5pkjnu4GHJ67K4RYH1ozZbspbGQF67-K4HQvtg9LDF8cKWAhVM1XcwbOF-uOaUQjq6vhEenYSP3VbShJsi-mVto_A\" alt=\"\"\u002F>\u003C\u002Ffigure>\n\u003C!-- \u002Fwp:image -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>Most times, lists are the perfect solution for presenting information from a database or a REST-based query. However, there are times when a list needs to provide \u003Cstrong>context\u003C\u002Fstrong> as to how records relate to each other. This is when organizing data as a graph becomes handy.\u003C\u002Fp>\n\u003C!-- \u002Fwp:paragraph -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>With graphs, the primary objective isn't to list information (although entirely possible) but to define the \u003Cstrong>relationship\u003C\u002Fstrong> between objects.&nbsp; Why would that be useful?\u003C\u002Fp>\n\u003C!-- \u002Fwp:paragraph -->\n\n\u003C!-- wp:image {\"align\":\"center\"} -->\n\u003Cfigure class=\"wp-block-image aligncenter\">\u003Cimg src=\"https:\u002F\u002Flh3.googleusercontent.com\u002FYKU9e5rjv7TiFzr64mfQL4xg9VGr_5Rx3xF5kX0wX_J5pod23qDDDddEQhbcQyeF0JWtyiyj5XIHqY5VzpfOK7rN_gkRv9_d6objX1_G8ufOMF7IHBPjqEX7-lDP6ndtGGEHPBUtfcVztkXU1Q\" alt=\"An undirected graph with two vertices and one edge.\"\u002F>\u003Cfigcaption>\u003Cem>An undirected graph with two vertices and one edge.\u003C\u002Fem>\u003C\u002Ffigcaption>\u003C\u002Ffigure>\n\u003C!-- \u002Fwp:image -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>\u003Cstrong>Mapping applications\u003C\u002Fstrong>: How would you organize data needed to recreate a mapping service like Apple or Google Maps if asked in a technical interview? More than just providing a list of all known roads in a database, your model would need to determine the best way to reach a destination based on the time of day, traffic, and one-way streets among other factors. For this considerable volume of data to be effective, you need to know how a single road \u003Cstrong>relates\u003C\u002Fstrong> to all other streets in the model.&nbsp;&nbsp;\u003C\u002Fp>\n\u003C!-- \u002Fwp:paragraph -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>\u003Cstrong>Social media:\u003C\u002Fstrong>\u003Cstrong> \u003C\u002Fstrong>Success on social media is often measured by the number of people you follow or that follow you. Networking platforms like Twitter attract massive audiences by allowing you to connect with anyone, permitting you to receive their latest updates. The LinkedIn model is more detailed as you cannot add someone to your network unless the receiver accepts your connection request. In this case, LinkedIn connections represent two-way \u003Cstrong>relationships\u003C\u002Fstrong>. Taking this idea further, you can also search to see if anyone in your network is connected to a job opportunity you want to pursue. In this case, \"network\" could imply a direct or indirect connection. Such a robust model isn't just based on a simple list but would include the smarts to determine how all profiles relate.&nbsp;\u003C\u002Fp>\n\u003C!-- \u002Fwp:paragraph -->\n\n\u003C!-- wp:heading -->\n\u003Ch2 id=\"h-graph-components\">Graph components\u003C\u002Fh2>\n\u003C!-- \u002Fwp:heading -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>Now that we've seen how graphs are used in everyday applications let's introduce their components. Nodes in a graph are referred to as vertices. While it would be possible to build a graph as a single vertex, models that contain multiple vertices better represent real-world applications. Graph objects relate to one another through connections called edges. Depending on your requirements, a vertex could be connected to one or more things through edges. It's also possible to create a vertex without edges. Finally, unlike other standard structures like stacks or queues, graphs often have no designated start or end point. Here are some example graph configurations:\u003C\u002Fp>\n\u003C!-- \u002Fwp:paragraph -->\n\n\u003C!-- wp:image {\"align\":\"center\"} -->\n\u003Cfigure class=\"wp-block-image aligncenter\">\u003Cimg src=\"https:\u002F\u002Flh3.googleusercontent.com\u002FYKU9e5rjv7TiFzr64mfQL4xg9VGr_5Rx3xF5kX0wX_J5pod23qDDDddEQhbcQyeF0JWtyiyj5XIHqY5VzpfOK7rN_gkRv9_d6objX1_G8ufOMF7IHBPjqEX7-lDP6ndtGGEHPBUtfcVztkXU1Q\" alt=\"An undirected graph with two vertices and one edge.\"\u002F>\u003Cfigcaption>\u003Cem>An undirected graph with two vertices and one edge.\u003C\u002Fem>\u003C\u002Ffigcaption>\u003C\u002Ffigure>\n\u003C!-- \u002Fwp:image -->\n\n\u003C!-- wp:image {\"align\":\"center\"} -->\n\u003Cfigure class=\"wp-block-image aligncenter\">\u003Cimg src=\"https:\u002F\u002Flh4.googleusercontent.com\u002FVcPqjiEZBAXpdFzareIAEkHDt4JvW0JIn9l5QXoiLBAXApH9nSKQ-lkCqW0pWKwpMcGXt2o-b_x2bqQKdycex-ccLfLTvC3dQtgCbgyd6stMTnlpOgQi_1ExrJDdoG7hO04YpdZxFoXqE7_HSg\" alt=\"An undirected graph with four vertices and three edges.\"\u002F>\u003Cfigcaption>\u003Cem>An undirected graph with four vertices and three edges.\u003C\u002Fem>\u003C\u002Ffigcaption>\u003C\u002Ffigure>\n\u003C!-- \u002Fwp:image -->\n\n\u003C!-- wp:image {\"align\":\"center\"} -->\n\u003Cfigure class=\"wp-block-image aligncenter\">\u003Cimg src=\"https:\u002F\u002Flh3.googleusercontent.com\u002FA_lgQTRuMXS-_mk0r-daVSlcWWxehgY33xjCpCF5vkYqwTlP-XD_IOb5RikGoKycZWYsHb4x7JAG2hMzO84ARnyKKjwUzxOTUsXzReY87m7k3BcEN6pxxkPjx8lbd2QjpWnSZKIqPqaTxULCDw\" alt=\"An undirected graph with three vertices and three edges.\"\u002F>\u003Cfigcaption>\u003Cem>An undirected graph with three vertices and three edges.\u003C\u002Fem>\u003C\u002Ffigcaption>\u003C\u002Ffigure>\n\u003C!-- \u002Fwp:image -->\n\n\u003C!-- wp:heading -->\n\u003Ch2 id=\"h-directed-vs-undirected\">Directed vs. undirected\u003C\u002Fh2>\n\u003C!-- \u002Fwp:heading -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>In an undirected graph, the connection between a source vertex and destination are equal. These models represent two-way connections—similar to a two-way street in the mapping application. To define a connection going a single direction, we can update our model to a \u003Cstrong>directed\u003C\u002Fstrong> graph by using lines and arrows:\u003C\u002Fp>\n\u003C!-- \u002Fwp:paragraph -->\n\n\u003C!-- wp:image {\"align\":\"center\"} -->\n\u003Cfigure class=\"wp-block-image aligncenter\">\u003Cimg src=\"https:\u002F\u002Flh4.googleusercontent.com\u002FMSu8-PVDV2lCpIwaD9kH4RkCG-MIqfLZSnogpGuVdeLrhIj_46dAEbR07W1kCARt5pW2-2HwtllWZr0UxOj-INbv-uK8v-lqWXdX7Nrtw3Bj62TKWPRdBTZB5QqaBQkzWPbOvu1RygHpFl_lrw\" alt=\"A directed graph with three vertices and three edges.\"\u002F>\u003Cfigcaption>\u003Cem>A directed graph with three vertices and three edges.\u003C\u002Fem>\u003C\u002Ffigcaption>\u003C\u002Ffigure>\n\u003C!-- \u002Fwp:image -->\n\n\u003C!-- wp:heading -->\n\u003Ch2 id=\"h-level-of-connectedness\">Level of connectedness\u003C\u002Fh2>\n\u003C!-- \u002Fwp:heading -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>Sometimes, we must represent the level of connectedness between vertices in a graph. This technique works well when quantifying distances, times, or severity between nodes. Generally associated with an edge, the \u003Cstrong>weight\u003C\u002Fstrong> is a comparative variable tracked for this purpose.&nbsp;\u003C\u002Fp>\n\u003C!-- \u002Fwp:paragraph -->\n\n\u003C!-- wp:image {\"align\":\"center\"} -->\n\u003Cfigure class=\"wp-block-image aligncenter\">\u003Cimg src=\"https:\u002F\u002Flh4.googleusercontent.com\u002FjtgRbQXnkClTuFI6ka55qbmWL7xzU0OGl4cCoKeHTSee7g9vjNLD33w7HQJxUxBaiGa-3HB7PEZMhmyMXdlhJZPkhaJjaZXYgAn62rCCUX0b1HdgW1QAQq6cbNHoNFCeer7-cJQYjwspf6CCyw\" alt=\"A directed graph with three vertices and three edges where the edges are weighted.\"\u002F>\u003Cfigcaption>\u003Cem>A directed graph with three vertices and three edges where the edges are weighted.\u003C\u002Fem>\u003C\u002Ffigcaption>\u003C\u002Ffigure>\n\u003C!-- \u002Fwp:image -->\n\n\u003C!-- wp:heading -->\n\u003Ch2 id=\"h-graph-vertex\">Graph vertex\u003C\u002Fh2>\n\u003C!-- \u002Fwp:heading -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>With a basic understanding of graph theory in place, let's see how to replicate some of these models in code. Below we've created a vertex that supports a custom generic object (\u003Ccode>T\u003C\u002Fcode>). The \u003Ccode>tvalue\u003C\u002Fcode> variable represents the data held by the type, including a single string, int, or custom type (for example., street name or social media profile). Also, note our class conforms to the popular Equatable protocol (Swift). This will allow us to compare specific vertex instances for equality if required.&nbsp;&nbsp;\u003C\u002Fp>\n\u003C!-- \u002Fwp:paragraph -->\n\n\u003C!-- wp:code -->\n\u003Cpre class=\"wp-block-code\">\u003Ccode>public class Vertex &lt;T&gt; : Equatable {\n   \t \n\tvar tvalue: T?\n\tvar neighbors = Array&lt;Edge&lt;T&gt;&gt;()\n\tlet uuid = UUID()\n        \n\tpublic init(with name: T) {\n   \tself.tvalue = name\n\t}\n   \t \n\t\u002F\u002Fequatable conformance\n\tpublic static func == (lhs: Vertex, rhs: Vertex) -&gt; Bool {\n    \treturn lhs.uuid == rhs.uuid\n\t}\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003C!-- \u002Fwp:code -->\n\n\u003C!-- wp:heading -->\n\u003Ch2 id=\"h-adjacency-lists\">Adjacency lists\u003C\u002Fh2>\n\u003C!-- \u002Fwp:heading -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>The neighbors property represents the connections made to other vertices. As discussed, each vertex can connect to one or more neighbors. This list of relationships is sometimes called an “adjacency list” and can be used to solve many advanced problems.&nbsp;\u003C\u002Fp>\n\u003C!-- \u002Fwp:paragraph -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>\u003Ccode>var neighbors = Array&lt;Edge&lt;T&gt;&gt;()\u003C\u002Fcode>\u003C\u002Fp>\n\u003C!-- \u002Fwp:paragraph -->\n\n\u003C!-- wp:heading -->\n\u003Ch2 id=\"h-graph-edge\">Graph edge\u003C\u002Fh2>\n\u003C!-- \u002Fwp:heading -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>We added a neighbor property to store an array of custom edge types when creating our vertex. Below, an edge provides a reference for a subsequent neighboring vertex and its potential edge weight value.&nbsp;\u003C\u002Fp>\n\u003C!-- \u002Fwp:paragraph -->\n\n\u003C!-- wp:code -->\n\u003Cpre class=\"wp-block-code\">\u003Ccode>public class Edge &lt;T&gt; {\n    \n\tvar neighbor: Vertex&lt;T&gt;\n\tvar weight: Int\n    \n\tinit() {\n    \tweight = 0\n    \tself.neighbor = Vertex&lt;T&gt;()\n\t}\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003C!-- \u002Fwp:code -->\n\n\u003C!-- wp:heading -->\n\u003Ch2 id=\"h-building-the-canvas\">Building the canvas\u003C\u002Fh2>\n\u003C!-- \u002Fwp:heading -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>With our vertex and edge objects in place, we can now add them to our central storage structure we'll call the graph canvas. Even though our canvas is technically an array, the goal is to \u003Cstrong>visualize\u003C\u002Fstrong> the collection as a set of relationships. The \u003Ccode>addVertex\u003C\u002Fcode> function allows us to add a single generic vertex to the canvas, while the \u003Ccode>addEdge\u003C\u002Fcode> method provides reference information needed for an edge. Lastly, our code assumes the graph is directed, as the edge is (only) added to the source vertex adjacency list.\u003C\u002Fp>\n\u003C!-- \u002Fwp:paragraph -->\n\n\u003C!-- wp:code -->\n\u003Cpre class=\"wp-block-code\">\u003Ccode>public class Graph &lt;T&gt; {\n   \n\tvar canvas: Array&lt;Vertex&lt;T&gt;&gt;\n  \t \n   public init() {\n    \tcanvas = Array&lt;Vertex&gt;()\n\t}\n    \n\t\u002F\u002Fadd vertex to graph canvas\n\tpublic func addVertex(element: Vertex&lt;T&gt;) {\n    \tcanvas.append(element)\n\t}\n    \n      \u002Fadd edge\n\tpublic func addEdge(source: Vertex&lt;T&gt;, neighbor: Vertex&lt;T&gt;, weight: Int) {\n   \t \n    \t\u002F\u002Fcreate a new edge\n    \tlet newEdge = Edge&lt;T&gt;()\n   \t \n   \t   \u002F\u002Fconnect source vertex to neighboring edge\n    \tnewEdge.neighbor = neighbor\n    \tnewEdge.weight = weight\n   \t\t \n    \tsource.neighbors.append(newEdge)\n\t}\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003C!-- \u002Fwp:code -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>In summary, we've introduced graphs and have seen how they are used to represent the relationship between objects. We also reviewed a few ways to configure a graph and the components used to describe different models. With our model defined, we've set the stage for more advanced functionality, including graph navigation and traversal algorithms like breadth-first search.\u003C\u002Fp>\n\u003C!-- \u002Fwp:paragraph -->","html","2022-05-26T14:00:00.000Z",{"current":417},"the-complete-beginners-guide-to-graph-theory",[419,427,432,436],{"_createdAt":420,"_id":421,"_rev":422,"_type":423,"_updatedAt":420,"slug":424,"title":426},"2023-05-23T16:43:21Z","wp-tagcat-code-for-a-living","9HpbCsT2tq0xwozQfkc4ih","blogTag",{"current":425},"code-for-a-living","Code for a Living",{"_createdAt":420,"_id":428,"_rev":422,"_type":423,"_updatedAt":420,"slug":429,"title":431},"wp-tagcat-graph-theory",{"current":430},"graph-theory","graph theory",{"_createdAt":420,"_id":433,"_rev":422,"_type":423,"_updatedAt":420,"slug":434,"title":435},"wp-tagcat-maps",{"current":435},"maps",{"_createdAt":420,"_id":437,"_rev":422,"_type":423,"_updatedAt":420,"slug":438,"title":440},"wp-tagcat-swift",{"current":439},"swift","Swift","The complete beginner's guide to graph theory",[443,449,455,461],{"_id":444,"publishedAt":445,"slug":446,"sponsored":88,"title":448},"76c9771b-34e6-4d98-8641-ecefc711f0ef","2026-07-06T15:23:34.559Z",{"_type":10,"current":447},"when-the-sensor-starts-thinking-snortml-agentic-ai-and-the-evolving-architecture-of-intrusion-detection","When the sensor starts thinking: SnortML, agentic AI, and the evolving architecture of intrusion detection",{"_id":450,"publishedAt":451,"slug":452,"sponsored":88,"title":454},"28e560af-f0aa-4d46-bd90-f435ad604aa7","2026-06-26T14:00:27.102Z",{"_type":10,"current":453},"paging-charity-how-can-engineering-leaders-avoid-becoming-bond-villains","Paging Charity! How can engineering leaders avoid becoming Bond villains?",{"_id":456,"publishedAt":457,"slug":458,"sponsored":88,"title":460},"4b22c2a3-3779-4966-93eb-5230391dbdce","2026-06-23T14:08:58.595Z",{"_type":10,"current":459},"your-ai-shipped-a-backend-that-boots-that-is-the-whole-problem","Your AI shipped a backend that boots. That is the whole problem.",{"_id":462,"publishedAt":463,"slug":464,"sponsored":88,"title":466},"5cf362e1-fe7b-45af-b69c-914731c6a052","2026-06-23T14:00:00.000Z",{"_type":10,"current":465},"the-2026-developer-survey-is-now-open-for-human-developers-only","The 2026 Developer Survey is now open (for human developers only)!",{"data":468,"sourceMap":-1},{"count":469,"lastTimestamp":88},0]