.net C# 實現任意List的笛卡爾乘積演算法代碼

來源:互聯網
上載者:User

可以擴充到多個集合的情況。類似的例子有,如果A表示某學校學生的集合,B表示該學校所有課程的集合,則A與B的笛卡爾積表示所有可能的選課情況

複製代碼 代碼如下:using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace 演算法
{
public static class 演算法
{
/// <summary>
/// 笛卡爾乘積
/// </summary>
public static List<List<T>> CartesianProduct<T>(this List<List<T>> lstSplit)
{
int count = 1;
lstSplit.ForEach(item => count *= item.Count);
//count = lstSplit.Aggregate(1, (result, next) => result * next.Count);

var lstResult = new List<List<T>>();

for (int i = 0; i < count; ++i)
{
var lstTemp = new List<T>();
int j = 1;
lstSplit.ForEach(item =>
{
j *= item.Count;
lstTemp.Add(item[(i / (count / j)) % item.Count]);
});
lstResult.Add(lstTemp);
}
return lstResult;
}
}

class Program
{
public static void Main()
{
StringDemo();
根據Sector產生Routing的Demo();
根據Sector產生Routing的Demo2();
}

/// <summary>
/// 簡單字串 笛卡爾乘積
/// </summary>
private static void StringDemo()
{
var lstSource = new List<List<string>>
{
new List<string>() { "A","B","C"},
new List<string>() { "D","E","F"},
new List<string>() { "G","H","I"},
};

var sw = new Stopwatch();
sw.Start();
var lstResult = lstSource.CartesianProduct();
Console.WriteLine(sw.Elapsed);
}

private static void 根據Sector產生Routing的Demo()
{
//預設允許輸入多個BookingClass,表示使用任意一個都可以。
var lstSectorDef = new List<Sector>
{
new Sector{ SeqNO=1, BookingClass="A/A1/A2"},
new Sector{ SeqNO=2, BookingClass="B/B1/B2"},
new Sector{ SeqNO=3, BookingClass="C/C1/C2"},
//.....數量不定
};

var sw = new Stopwatch();
sw.Start();

var lstSectorGroup = new List<List<Sector>>();
lstSectorDef.ForEach(item =>
{
var lstSector = new List<Sector>();
foreach (var bookingClass in item.BookingClass.Split('/'))
{
var sector = item.Clone();
sector.BookingClass = bookingClass;

lstSector.Add(sector);
}
lstSectorGroup.Add(lstSector);
});

var lstRouting = lstSectorGroup.CartesianProduct();

Console.WriteLine(sw.Elapsed);
}

private static void 根據Sector產生Routing的Demo2()
{
//預設允許輸入多個BookingClass,表示使用任意一個都可以。
var lstSectorDef = new List<Sector>
{
new Sector{ SeqNO=1, BookingClass="A1/A2/A3"},
new Sector{ SeqNO=2, BookingClass="B1/B2/B3"},
new Sector{ SeqNO=3, BookingClass="C1/C2/C3"},
//.....數量不定
};

var sw = new Stopwatch();
sw.Start();

var lstTemp = new List<List<string>>();
lstSectorDef.ForEach(item =>
{
lstTemp.Add(item.BookingClass.Split('/').ToList());
});

var lstBookingClassGroup = lstTemp.CartesianProduct();

var lstRouting = new List<List<Sector>>();
for (int i = 0; i < lstBookingClassGroup.Count; i++)
{
var lstSector = new List<Sector>();
for (int j = 0; j < lstSectorDef.Count; j++)
{
var sector = lstSectorDef[j].Clone();
sector.BookingClass = lstBookingClassGroup[i][j];
lstSector.Add(sector);
}
lstRouting.Add(lstSector);
}

Console.WriteLine(sw.Elapsed);
}

}

[DebuggerDisplay("Sector:SeqNO={SeqNO},BookingClass={BookingClass}")]
public class Sector
{
public int SeqNO { get; set; }
public string BookingClass { get; set; }

public Sector Clone()
{
return this.MemberwiseClone() as Sector;
}
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.